Retrieving XML path text values

Discussion in 'Java' started by Abraham Khalil, Jan 27, 2004.

  1. Trying to write a function to get all values from a XML path without using a
    third party XPath libraries

    Using jdk1.4 and wondering if it supports XPaths?
    If so would prefer that as a solution instead of writing a recursive one myself

    Thanks

    An example of a start that I'am trying below


    public static String[] getValue(Document doc, String path) throws Exception {
    String[] nodeNames = path.split("[\\/]");

    for (int i = 0 ; i < nodeNames.length ; i++) {
    String nodeName = nodeNames;
    NodeList elements = doc.getElementsByTagName(nodeName);
    }
    }
    Abraham Khalil, Jan 27, 2004
    #1
    1. Advertising

  2. Abraham Khalil

    Adam Jenkins Guest

    Abraham Khalil wrote:
    > Trying to write a function to get all values from a XML path without using a
    > third party XPath libraries
    >
    > Using jdk1.4 and wondering if it supports XPaths?
    > If so would prefer that as a solution instead of writing a recursive one myself


    Unfortunately there is no standard way to apply an XPath expression to
    an org.w3c.dom.Node. However, the implementation of javax.xml.* that
    ships with Sun's JDK is Xalan, which does include nice XPath support.
    So if you're using Sun's JDK, you can write code like

    import org.apache.xpath.XPathAPI;
    import org.w3c.dom.*;

    // find the first node matching an XPath expression in
    // the subtree starting at "node"
    Node getNode(Node node, String xpathExpr) throws Exception {
    return XPathAPI.selectSingleNode(node, xpathExpr);
    }

    // get the string value of an XPath expression
    String getNodeValue(Node node, String xpathExpr) throws Exception {
    return XPathAPI.eval(node, xpathExpr).str();
    }

    Even if you are using a JDK that doesn't come with Xalan, you can get it
    free from http://xml.apache.org. The license allows it to be used in
    commercial products, so I see no reason to write your own XPath
    interpreter, except for fun.

    > An example of a start that I'am trying below
    >
    >
    > public static String[] getValue(Document doc, String path) throws Exception {
    > String[] nodeNames = path.split("[\\/]");
    >
    > for (int i = 0 ; i < nodeNames.length ; i++) {
    > String nodeName = nodeNames;
    > NodeList elements = doc.getElementsByTagName(nodeName);
    > }
    > }


    That's a start, but I bet pretty soon you'll want to evaluate more
    complex XPath expression, and next thing you know you'll be implementing
    your own XPath interpreter. Unless you're doing it for fun, I don't see
    the point since Xalan works well already.

    Adam
    Adam Jenkins, Jan 27, 2004
    #2
    1. Advertising

  3. Check out this old article.

    http://www.fawcette.com/xmlmag/2001_12/magazine/columns/javabreak/

    (watch the wrap)

    "Abraham Khalil" <> wrote in message
    news:...
    > Trying to write a function to get all values from a XML path without using

    a
    > third party XPath libraries
    >
    > Using jdk1.4 and wondering if it supports XPaths?
    > If so would prefer that as a solution instead of writing a recursive one

    myself
    >
    > Thanks
    >
    > An example of a start that I'am trying below
    >
    >
    > public static String[] getValue(Document doc, String path) throws

    Exception {
    > String[] nodeNames = path.split("[\\/]");
    >
    > for (int i = 0 ; i < nodeNames.length ; i++) {
    > String nodeName = nodeNames;
    > NodeList elements = doc.getElementsByTagName(nodeName);
    > }
    > }
    Patrick Thrapp, Jan 27, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kathy Burke
    Replies:
    0
    Views:
    332
    Kathy Burke
    Aug 4, 2003
  2. =?Utf-8?B?RGVhbiBNY0NhcnRlbg==?=

    Retrieving UNC Path of Mapped Drives on Client Machine

    =?Utf-8?B?RGVhbiBNY0NhcnRlbg==?=, Apr 30, 2004, in forum: ASP .Net
    Replies:
    7
    Views:
    5,569
    =?Utf-8?B?RGVhbiBNY0NhcnRlbg==?=
    May 20, 2004
  3. Jon Pope

    Retrieving virtual path of web service

    Jon Pope, Aug 8, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,095
    Brock Allen
    Aug 9, 2005
  4. Lorin Hochstein

    Retrieving the full path of Unix apps

    Lorin Hochstein, Oct 5, 2004, in forum: Python
    Replies:
    7
    Views:
    1,672
    Andrew Dalke
    Oct 6, 2004
  5. Rob Cowie

    Retrieving Filename from Path

    Rob Cowie, Aug 31, 2005, in forum: Python
    Replies:
    2
    Views:
    360
    Rob Cowie
    Aug 31, 2005
Loading...

Share This Page