How to parse org.w3c.dom.Element RPX XML response
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>
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() : "";
精彩评论