• 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

XPath for non-XML

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
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
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
}
reply
    Bookmark Topic Watch Topic
  • New Topic