• 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

XML Searching via java

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone out there have any ideas on searching an XML document using Java. I want to be able to search for a specific word in any part of the XML document, nodes, atributes, text, cdata....
What is the best way to do this?
Is there anything already written to do this type of searching?
I have a java class which starts at the beginning and recursive looks at each part of the xml document, but is there a better way?
Any ideas would be great?
Thanks,
Dean
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd think SAX will be a good tool for this kind of searching. Although I'm tempted to suggest DOM or JDOM, the amount of recursion you're talking about can take a toll on performance for large documents.
Simply program a ContentHandler that does the text pattern match and when the match is found, try to interpret the location referencen and store the results in your own datastructure. Since you are talking about *all kinds* of elements in the document, you will have to make provisions in the data structure to store the type of the element when a match is found.
Hope this helps!
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dean,
I had done something exactly like Ajith had suggested here, few months ago.
Use a document builder to parse the file and iterate thro the contents starting from root. By doing so, you can examine every node and match it whatever you are looking for.
Hope this helps.
regards
Meera
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with the SAX approach, so long as the context of the datum is not important. However, if you need to find an element that is subordinate to another element, or belongs to a particular structure, then this will be very difficult with SAX.
To search for, say, "an ID within a PO" rather than just any occurance of "ID", I would use an object tree (DOM or JDOM) and use an XPath expression to find the node (or nodes) that qualify.
-S-
 
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic