• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XML Searching via java

 
Dean Reedy
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • 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
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • 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!
 
Meera Chandrasekaran
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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
 
Scott Bain
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • 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-
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic