wsxedc
wsxedc

Reputation: 123

Java reading xml element without prefix but within the scope of a namespace

Functionally, the two blocks should be the same

<soapenv:Body>
  <ns1:login xmlns:ns1="urn:soap.sof.com">
    <userInfo>
      <username>superuser</username>
      <password>qapass</password>
    </userInfo>
  </ns1:login>
</soapenv:Body>

-----------------------

<soapenv:Body>
  <ns1:login xmlns:ns1="urn:soap.sof.com">
    <ns1:userInfo>
      <ns1:username>superuser</ns1:username>
      <ns1:password>qapass</ns1:password>
    </ns1:userInfo>
  </ns1:login>
</soapenv:Body>

However, how when I read using AXIS2 and I have tested it with java6 as well, I am having a problem.

 MessageFactory factory = MessageFactory.newInstance();
 SOAPMessage soapMsg = factory.createMessage(new MimeHeaders(), SimpleTest.class.getResourceAsStream("LoginSoap.xml"));

 SOAPBody body = soapMsg.getSOAPBody();

 NodeList nodeList = body.getElementsByTagNameNS("urn:soap.sof.com", "login");
 System.out.println("Try to get login element" + nodeList.getLength());  // I can get the login element

 Node item = nodeList.item(0);
 NodeList elementsByTagNameNS = ((Element)item).getElementsByTagNameNS("urn:soap.sof.com", "username");
 System.out.println("try to get username element " + elementsByTagNameNS.getLength());

So if I replace the 2nd getElementsByTagNameNS with ((Element)item).getElementsByTagName("username");, I am able to get the username element. Doesn't username have ns1 namespace even though it doesn't have the prefix? Am I suppose to keep track of the namespace scope to read an element? Wouldn't it became nasty if my xml elements are many level deep? Is there a workaround where I can read the element in ns1 namespace without knowing whether a prefix is defined?

Upvotes: 0

Views: 3637

Answers (1)

Affe
Affe

Reputation: 47994

Short answer is no, those documents are not the same. Namespace is not inherited by elements, and in setting a prefix, your namespace no longer functions as the default namespace for the document.

These two would be the same:

<soapenv:Body>
  <login xmlns="urn:soap.sof.com">
    <userInfo>
      <username>superuser</username>
      <password>qapass</password>
    </userInfo>
  </login>
</soapenv:Body>

-----------------------

<soapenv:Body>
  <ns1:login xmlns:ns1="urn:soap.sof.com">
    <ns1:userInfo>
      <ns1:username>superuser</ns1:username>
      <ns1:password>qapass</ns1:password>
    </ns1:userInfo>
  </ns1:login>
</soapenv:Body>

For a more robust way to read the document, you should probably look into compiling some XPath statements. Namespace issues are only one of the problems with relying on the getElementsByTagName(NS) convenience methods.

-- Edit --

Xpath itself is pretty basic. e.g., //userInfo select all the userInfo elements at any level. //login/userInfo select all the userInfo elements that are children of a login at any level. Like everything else, it gets messier when you have to start adding name spaces.

private NamespaceContext ns = new NamespaceContext() {
public String getNamespaceURI(String prefix) {
if (prefix.equals("urn") return "urn:soap.sof.com";
else return XMLConstants.NULL_NS_URI;
}
public String getPrefix(String namespace) {
throw new UnsupportedOperationException();
}
public Iterator getPrefixes(String namespace) {
throw new UnsupportedOperationException();
}};

XPathFactory xpfactory = XPathFactory.newInstance();
XPath xpath = xpfactory.newXPath();
xpath.setNamespaceContext(ns);
NodeList nodes = (NodeList) xpath.evaluate("//urn:userInfo|//userInfo", myDom, XPathConstants.NODESET);
//find all userInfo at any depth with either namespace.

S'been a long time since I've used JAXP, but I think that's basically correct. Running an xPath isn't slow, but compiling them is. You can compile them to a XPathExpression for performance, but those aren't threadsafe, so you can't just cache them on a servlet. Never is easy =/.

If you are doing a lot of XML, I would recommend using Jaxen instead of JAXP. (On the other hand if you do very little XML and it's just a one of on the front end, maybe getElementsByTagName isn't the worst thing ever :) )

Upvotes: 1

Related Questions