aspose file tools*
The moose likes Beginning Java and the fly likes Stop parsing after a certain condition is met using DOM parser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Stop parsing after a certain condition is met using DOM parser" Watch "Stop parsing after a certain condition is met using DOM parser" New topic
Author

Stop parsing after a certain condition is met using DOM parser

ratna sargurp
Ranch Hand

Joined: Jan 12, 2010
Posts: 32
Hi

I am iterating through a number of XML files looking for a certain content. If found, I want to have the parser stop searching and parsing other files. Is there any way of doing this? I am using a DOM parser.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
Simple answer - No - not with DOM, the reason being that the DOM builder does not give you any intermediate results - it is a full DOM or failure.

The SAX/StAX approach gives you a chance to see every single element of the XML document and lets you stop whenever your conditions are met. Do a Google search for "java stax example."

In the interest of completeness, note that the DOM parser is actually built on top of a SAX parser.

Bill

ratna sargurp
Ranch Hand

Joined: Jan 12, 2010
Posts: 32
Thanks for your reply. I think had the requirement wrong.

What I need to do is:

1) iterate over a folder.
2) Parse each file in that folder.
3) As soon as condition is met, stop iterating and exit from the program.

So, my goal is to stop iterating over the folder once the condition is met. This is probably a beginner question(??) I am just blank at figuring this out.

Any help/tips?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
Since we are in an XML forum, I guess your "condition" is related to XML data elements, right?

How far have you gotten in parsing an XML document and determining if the condition has been met?

Bill
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

So this question has nothing to do with XML parsing at all, then. Your task is to look at files in a folder until you find one which satisfies a certain condition, and then to stop processing. Yes, that's a beginner question. Let me move the thread over there. And while I'm doing that, how about posting what you have so far? (Note: it's best to leave the XML processing aside for now, i.e. put it into a separate method.)
ratna sargurp
Ranch Hand

Joined: Jan 12, 2010
Posts: 32
Thanks for your replies. I have my DOM parser working where it parses the file that is passed to it and checks for the particular element.

Now, I am stepping back and looking at my code, and want to add that check of 'if element found, stop iterating over other files'.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

And why is that a problem?
ratna sargurp
Ranch Hand

Joined: Jan 12, 2010
Posts: 32
Its not a problem, except that I am racking my brains to get the logic right:-) esp. with the parsing.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

Well, again, the parsing has nothing to do with it. Your logic should be something like this:



As you see, it isn't very complicated. That's why I am at a loss to understand what your problem is.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8250
    
  23

Paul Clapham wrote:Well, again, the parsing has nothing to do with it. Your logic should be something like this:

And furthermore, if you put that logic inside a method, you can actually have it return something useful - like, for example, the name/path of the file that satisfied the condition.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
ratna sargurp
Ranch Hand

Joined: Jan 12, 2010
Posts: 32
Thanks for all your replies.

I wasn't thinking right. I got this part of the code working where I added logic to the method which was iterating. Its working now! :-)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

Glad to hear you got it straightened out.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Stop parsing after a certain condition is met using DOM parser