aspose file tools*
The moose likes XML and Related Technologies and the fly likes XPath for non-XML 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 "XPath for non-XML" Watch "XPath for non-XML" New topic
Author

XPath for non-XML

Jonas Bengtsson
Greenhorn

Joined: Oct 12, 2006
Posts: 4
Hi all,

I want to create my own DOM like data structure, and use XPath to locate nodes in my data structure.

It seems like javax.xml.xpath only supports XML documents, is that correct? Is there any other XPath libraries that supports custom data formats? All it ought to need is some interface for the nodes with getChildren(), getParent(), getType() (element, attribute, text etc), getValue() and perhaps a few more.

Thanks in advance,
Jonas
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12787
    
    5
The full implementation of XPath looks like a non-trivial exercise to me. By the time you create an object hierarchy that supports those interfaces you will have reinvented the wheel.

Whats wrong with transforming your data into a real XML document and working with the existing tools?

Bill
Jonas Bengtsson
Greenhorn

Joined: Oct 12, 2006
Posts: 4
Thanks for your reply Bill!

The reason is that I want to sub class the nodes. I.e. I don't want just data, I want to query live objects.

I have now tried to implement the interface javax.xml.dom.Node. And I can do a simple XPath query on that node:

(MyXPath.findNode() is just my wrapper around javax.xml.xpath.*)

But if I try to access I child I get ClassCastException:

The callstack:


Too bad I don't have the source code so I can't see what goes wrong.

Doesn't javax.xml.xpath.* support custom implementations of the interfaces? That seems a bit weird to be honest!

Cheers,
Jonas
[ October 12, 2006: Message edited by: Jonas Bengtsson ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12787
    
    5
What an interesting problem. Perhaps I am missing something but if your MyNode class implements org.w3c.Node, why:



why not


Bill
Jonas Bengtsson
Greenhorn

Joined: Oct 12, 2006
Posts: 4
It's somewhere between interesting and frustrating

I agree, it should be "Node parent = new MyNode("parent");", but I still get the same problem.

Cheers,
Jonas
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12787
    
    5
Yaknow - although I hate to say it - it looks like the author of that library screwed up and used a specific class instead of the interface at that point in the code.

Maybe it is time to explore outside the Java standard library, which is a real shame, but anyway.

You might find the XOM implementation of XPath more flexible. XOM is an open-source project by Elliotte Rusty Howard - who wrote the _Processing XML with Java_ "bible."

If you do dig into XOM, please keep us up to date with your findings

Bill
Jonas Bengtsson
Greenhorn

Joined: Oct 12, 2006
Posts: 4
Thanks for the recommendation!

However I think I found what I'm looking for in JaxenJaxen. In the FAQ they answer "How do I support a different object model?". Basically, there is a Navigator interface that all access goes through. So there is no need for using someone elses interface for my nodes. I haven't tried it out yet but I'm pretty confident it will work, and it's much easier than to implement my own XPath engine (I seriously considered doing that, with a Navigator interface much in the same way as Jaxen, earlier today )

Thanks for your assistance!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12787
    
    5
The thing I found most significant about that Jaxen reference is:
Sun chose Jaxen for the XPath engine for the JSP Standard Tag Library (JSTL) and the Web Services pack

Pretty impressive! Also I see that XOM 1.1 includes jaxen.

Let us know what you come up with, using XPath to query a structure of dynamic Java objects sounds interesting.

Bill
kal ath
Greenhorn

Joined: Nov 07, 2006
Posts: 1
Have a look at JXPath by Apache Jakarta. It seems to be exactly what you are looking for.

From the JXPath site:

The org.apache.commons.jxpath package defines a simple interpreter of an expression language called XPath. JXPath applies XPath expressions to graphs of objects of all kinds: JavaBeans, Maps, Servlet contexts, DOM etc, including mixtures thereof.

Consider this example:


Address address = (Address)JXPathContext.newContext(vendor).
getValue("locations[address/zipCode='90210']/address");


This XPath expression is equvalent to the following Java code:

Address address = null;
Collection locations = vendor.getLocations();
Iterator it = locations.iterator();
while (it.hasNext()){
Location location = (Location)it.next();
String zipCode = location.getAddress().getZipCode();
if (zipCode.equals("90210")){
address = location.getAddress();
break;
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XPath for non-XML