[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: XPath and Java DOM APIs


java dom xpath
Dare,

The Oracle XML Parser for Java supports a selectNodes(), selectSingleNode(),
and value() methods that support XPath 1.0

http://otn.oracle.com/tech/xml/xdk_java/content.html

The following program should answer your questions:

import java.io.*;
import oracle.xml.parser.v2.*;
import org.w3c.dom.*;
public class Test {
  private static final String DOC =
  "<A xmlns:glop='http://blah'>"+
  "<C xmlns='http://blah'>abc</C>"+
  "<glop:C>xyz</glop:C>"+
  "<glop:C xmlns:glop='http://example.com'>123</glop:C>"+
  "<C>456</C><B></B></A>";
  public static void main(String[] args) throws Throwable {
    DOMParser d = new DOMParser();
    d.parse( new StringReader(DOC));
    XMLDocument doc = d.getDocument();
    doc.print(System.out);
    NSResolver ns = new myResolver();
    NodeList nl = doc.selectNodes("//p:C", ns);
    System.out.println("Found " + nl.getLength() + " Elements...");
    for (int i = 0, c = nl.getLength(); i < c; i++) {
      Element e = (Element)nl.item(i);
      System.out.println(e.getNamespaceURI()+", "+e.getNodeName());
    }    nl = doc.selectNodes("//*[local-name()='C' and local-name() != name()]", ns);
    System.out.println("Found " + nl.getLength() + " Elements...");
    for (int i = 0, c = nl.getLength(); i < c; i++) {
      Element e = (Element)nl.item(i);
      System.out.println(e.getNodeName());
    }
  }

 static class myResolver implements NSResolver {
     public String resolveNamespacePrefix(String f) {
       if (f.equals("p")) return "http://blah";
       return null;
     }
  }
}

The output is:

Found 2 Elements...
http://blah, C
http://blah, glop:C

Found 2 Elements...
glop:C
glop:C

__________________________________________________________________
Steve Muench - Developer, Product Mgr, Java/XML Evangelist, Author
Simplify J2EE and EJB Development with BC4J
http://otn.oracle.com/products/jdev/htdocs/j2ee_bc4j.html
Building Oracle XML Apps, www.oreilly.com/catalog/orxmlapp
----- Original Message -----
From: "Dare Obasanjo" <kpako@y...>
To: <xml-dev@l...>
Sent: Saturday, July 27, 2002 10:50 PM
Subject:  XPath and Java DOM APIs


| A friend of mine told me that he couldn't find a Java
| DOM API that supported XPath which could successfully
| return
|
| a.) All the C elements from the http://blah namespace
|
| b.) All the C elements in documents that have a
| namespace name.
|
| from the XML document below.
|
| <?xml version="1.0" ?>
|  <A xmlns:glop="http://blah">
|   <C xmlns="http://blah">abc</C>
|   <glop:C>xyz</glop:C>
|   <glop:C xmlns:glop="http://example.com">123</C>
|   <C>456</C>
|   <B>
|   </B>
|  </A>
|
| I doubt that this is the case but couldn't contradict
| his claims after doing a few cursory searches on
| Google.
|
| Can anyone confirm or deny this and provide sample
| code if it is the latter?
|
| Thanks.
|
| PS: I think his only caveat is that it uses Java 1.3
| not Java 1.4.
|
| =====
| THINGS TO DO IF I BECOME AN EVIL OVERLORD #79
| If my doomsday device happens to come with a reverse switch, as soon as it has been employed it will be melted down and made into
limited-edition commemorative coins.
|
| __________________________________________________
| Do You Yahoo!?
| Yahoo! Health - Feel better, live better
| http://health.yahoo.com
|
| -----------------------------------------------------------------
| The xml-dev list is sponsored by XML.org <http://www.xml.org>, an
| initiative of OASIS <http://www.oasis-open.org>
|
| The list archives are at http://lists.xml.org/archives/xml-dev/
|
| To subscribe or unsubscribe from this list use the subscription
| manager: <http://lists.xml.org/ob/adm.pl>
|
|


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.