Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 798091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:54:55+00:00 2026-05-14T22:54:55+00:00

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>

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:54:56+00:00Added an answer on May 14, 2026 at 10:54 pm

    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 🙂 )

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would you go about proving that two queries are functionally equivalent, eg they
Is there in Ruby some functionality/syntax to compare two floats with delta? Something similar
I need advice on what I need to do to make the following two
I'm new to Doctrine and ActiveRecord. How should I filter a table after it
My program that I am writing's purpose arose with this issue: There are two
The Clojure API describes these two functions as: (send a f & args) -
Functional languages are good because they avoid bugs by eliminating state, but also because
Functional programming seems to be a paradigm in computer science which has more and
Functional programming .. is like classic ( Mark Twain's type ). While reading another
What functionality does the yield keyword in Python provide? For example, I'm trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.