• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Stop parsing after a certain condition is met using DOM parser

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And why is that a problem?
 
ratna sargurp
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its not a problem, except that I am racking my brains to get the logic right:-) esp. with the parsing.
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
ratna sargurp
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad to hear you got it straightened out.
 
I'm doing laundry! Look how clean this tiny ad is:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic