[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] RE: How to parse XML document with default namespace with JDOM
The book you are quoting is very old information. Where it
says that you can do something, it's probably right, but where it says that you
can't, it may well be wrong.
Frankly, I forget exactly what's in Saxon 6.5.x because
it's so long ago - I know JDOM was already supported back then but I don't
remember the details of the API. I do recall, that as Elliotte says in his book,
the Saxon API for invoking XPath was pretty clumsy in those days (because it was
designed primarily for internal use by the XSLT engine, not as a user-facing
interface). I would use a more recent release.
But the code you gave us wasn't even trying to
use Saxon, it was using the XPath engine within JDOM, using a JDOM API that
I'm not very familiar with, and therefore I can't tell you why it isn't
working.
My own preference for this kind of coding would be to use
Saxon's s9api interface, documented at
http://www.saxonica.com/documentation/javadoc/net/sf/saxon/s9api/package-summary.html
There are sample applications in the saxon-resources
download (from SourceForge). It includes an example of XPath with JDOM like
this:
public
void run() throws SaxonApiException
{
// Build the JDOM document org.jdom.input.SAXBuilder jdomBuilder = new org.jdom.input.SAXBuilder(); File file = new File("data/books.xml"); org.jdom.Document doc; try { doc = jdomBuilder.build(file); } catch (org.jdom.JDOMException e) { throw new SaxonApiException(e); } catch (IOException e) { throw new SaxonApiException(e); } Processor proc = new Processor(false); DocumentBuilder db = proc.newDocumentBuilder(); XdmNode xdmDoc = db.wrap(doc); XPathCompiler xpath = proc.newXPathCompiler(); XPathExecutable xx = xpath.compile("//ITEM/TITLE"); XPathSelector selector = xx.load(); selector.setContextItem(xdmDoc); for(XdmItem item : selector) { XdmNode node = (XdmNode)item; org.jdom.Element element = (org.jdom.Element)node.getExternalNode(); System.out.println(element.getValue()); } } (The method getExternalNode() was added in Saxon 9.1.0.2
and is not yet in the published Javadoc)
You would probably want to add a call
xpath.declareNamespace("prefix", "uri")
before the compile() call.
Michael Kay
http://www.saxonica.com/
From: Jack Bush
[mailto:netbeansfan@y...]
Sent: 05 November 2008 03:48 To: Michael Kay; xml-dev@l... Subject: Re: How to parse XML document with default namespace with JDOM XPath
[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index] |
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|