tags:

views:

32

answers:

1

I am using rpxnow in Java, how do I use org.w3c.dom API to get the field identifier in this XML reponse for example?

<?xml version='1.0' encoding='UTF-8'?>
    <rsp stat='ok'>
      <profile>
        <displayName>
          brian
        </displayName>
        <identifier>
          http://brian.myopenid.com/
        </identifier>
        <preferredUsername>
          brian
        </preferredUsername>
        <providerName>
          Other
        </providerName>
        <url>
          http://brian.myopenid.com/
        </url>
      </profile>
    </rsp>
A: 

Did you have a look at the API documentation of the package org.w3c.dom? Class Element contains methods such as getElementsByTagName(String name) to get child elements with a specific tag name (and other methods that might be useful). Using this API is a bit cumbersome, though. You could use XPath (see package javax.xml.xpath) to do it with fewer lines of code. For example (untested):

Element element = ...;

XPath xpath = XPathFactory.newInstance().newXPath();
Node node = (Node) xpath.evaluate("/rsp[@stat='ok']/profile/identifier", element, XPathConstants.NODE);

String value = node != null ? node.getTextContent().trim() : "";
Jesper