File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Using XPath to read list of nodes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Using XPath to read list of nodes" Watch "Using XPath to read list of nodes" New topic
Author

Using XPath to read list of nodes

Rick Crawford
Ranch Hand

Joined: Aug 17, 2001
Posts: 34
Hello all. I'm trying to use XPath expressions to read an XML file. My basic question is, how do I create a method that uses XPath to position a 'pointer' to a section within an XML document, then iterate through 'child' and 'grandchild' nodes looking for desired values?
Here is the relevant subset of the file:

I wrote (actually, copied from somewhere, probably the Moose) a little helper method to look for data elements within the XML:

..and I use the method like this:

Everything works pretty well if the section I'm looking for occurs only once in the file. But I want to access the attributes FONTFACE, FONTSIZE, XPOS, YPOS and TEXT within each MESSAGE section. I suppose could rename the MESSAGE nodes to MESSAGE1, MESSAGE2, MESSAGE3, then path them in explicitly, but I'd prefer to leave it flexible enough that I could add a fourth MESSAGE.
I realize that I couldn't return all five child elements at once. But I figured I'd create another class with member variables for each element and use that for the return value.
Thanks in advance for any help,
Rick <><
Hari babu
Ranch Hand

Joined: Jun 25, 2001
Posts: 208
Hi,
Iam not sure what u are trying to get but from my understanding of u'r question try this
//MESSAGES/TEXT[contains(.,"SEE ATTACHED")
This returns the "whole" of the first Message Node
Hari
Rick Crawford
Ranch Hand

Joined: Aug 17, 2001
Posts: 34
Hari,
Thanks for the reply. One thing that I didn't make very clear is that FONTFACE, FONTSIZE, XPOS, etc. are nodes that are common for several data items that are not shown in my example, not just the MESSAGES nodes. The entire XML configuration file is pretty big, but here are a few more nodes in addition to the MESSAGES section:

What I'd like to do is access this data by the high level node name, such as SHIPPERNAME, then pick out these common fields and store them in a structure (an inner class.) Right now, I'm retrieving the values with multiple calls to my 'getVal' method, like this:

..and so on. But what I'd rather do is create some sort of a method call that would interrogate all the nodes within the '/MAINPAGEOVERLAY/SHIPPERNAME', looking for the named values of FONTFACE, FONTSIZE, etc., storing them within a structure:

My imaginary method signature (that I can't figure out how to write):

I would then use the values from the returned printPosition object. And as I alluded to in the previous message, the high level node, such as MESSAGES, could contain lower level nodes such as MESSAGE. But at some point when the lowest level has been reached, the common attributes of FONT and XPOS would be found.
This is all pretty complicated, and I apologize if I'm not being very clear on this. As always, I sincerely appreciate the help.
Rick <><
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using XPath to read list of nodes
 
Similar Threads
Submit from applet! Possible to do like this?
Text with Mouse
javax.xml Flush XML in hashtable
XML Parsing Issue
Java Web Services 2