• 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

Searching XML

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please can some one help. I have a web application which reads and XML file from disk into a org.w3c.Document object. Once I have the Document object I want to be able to write a method as follows:

public Document getDocumentSatisfyingCriteria(Document initialDom, String criteria){

// In here I wish to write logic that searches the initalDom object for all occurrences of the criteria string. It then copies all those nodes consiting of the criteria string in their 'Text Value' into another Document object and returns it.

}


Any ideas on the best approach to use? And cde snippet woould be useful.
 
Ranch Hand
Posts: 502
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
XPATH fits your need. XPATH is used to select particular node in XML DOM(Document) by giving search criteria. After getting node object, you can create a empty document and add those nodes to newly created document object.

For this I use dom4j library which supports DOM,SAX and XPATH.
 
Gobind Singh
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have taken your advice and plugged in dom4j.
Something you could maybe help me on:
I have a simple document as follows:
<phonebook>
<row>
<name>Neil</name>
<address>London</address>
</row>
<row>
<name>Benjamin</name>
<address>Toronto</address>
</row>
<row>
<name>Phillip</name>
<address>Moscow</address>
</row>
</phonebook>

I am using the following code snippet to traverse the dom using XPath:

XPath xpathSelector = DocumentHelper.createXPath("/phonebook/row/address");
List results = xpathSelector.selectNodes(document);

This works fine. But I need an xpath expression that will return all <row> nodes containing 'Moscow' either as <name> or as <address>. I dont want to restrict the Xpath to just one element. I want ito to return me all the rows where 'moscow' is present in ANY one of the children.
[ December 01, 2006: Message edited by: Gurps Bassi ]
 
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
Note that it is NOT necessary to use any additional library. If you have Java 1.5, xpath is included.

Bill
 
Prabhu Venkatachalam
Ranch Hand
Posts: 502
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this XPATH will fetch all row's child address which conaticn value as "Moscow"

row[address = "Moscow"]

Let me know, if this works.
 
Gobind Singh
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No what I want to do is a full text scan of the xml document. I want to search for a given string in the entire document. And I want it to return to me all the nodes that contain that string as their text context. The string may occur in the <address> element or the <name> element or any other element. That Is why I dont want to specify the element name.
 
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
Rather a different problem than what we were thinking of.

You will just have to examine every Element that has a Text node child - the method you choose to traverse the document will depend on the order of Elements you want the output document to have.

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
The XPath for that request would be something likewouldn't it?
 
Prabhu Venkatachalam
Ranch Hand
Posts: 502
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes this will do. XPATH Paul mentioned will search in all the child of root element for the text value "Moscow".
 
Gobind Singh
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes thank you, that Xpath expression works great.

Is there a LIKE operator in Xpath?

Also, How do I tell it to ignore case?

[ December 04, 2006: Message edited by: Gurps Bassi ]
[ December 04, 2006: Message edited by: Gurps Bassi ]
 
If I'd had more time, I would have written a shorter letter. -T.S. Eliot such a short, tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic