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

  • SEARCH
  • Home
  • 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 8792829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:02:15+00:00 2026-06-13T23:02:15+00:00

I want to get the value out tag2 and I got this a xml:

  • 0

I want to get the value out tag2 and I got this a xml:

String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
            "<ns1:schema xmlns:ns1='http://example.com'>" +
                "<ns1:tag1>" +
                "    <ns1:tag2>value</ns1:tag2>" +
                "</ns1:tag1>" +
            "</ns1:schema>"; 

Then parse it to a document and want to get the elements by tagnameNS. But when I run this the nodelist is empty why?

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        docBuilderFactory.setNamespaceAware(true);
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(new InputSource(new StringReader(xml))); 

        NodeList nl = doc.getElementsByTagNameNS("http://example.com", "tag2");

        String a = nl.item(0).getNodeValue();

Still doesnt work with the URI.

  • 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-06-13T23:02:16+00:00Added an answer on June 13, 2026 at 11:02 pm

    getElementsByTagNameNS is returing a result. The issue is that you’re currently calling the wrong method to get the text content off the result elements. You need to call getTextContext() and not getNodeValue()

    String a = nl.item(0).getTextContent();
    

    DomDemo

    Below is a complete code example.

    package forum13166195;
    
    import java.io.StringReader;
    import javax.xml.parsers.*;
    import org.w3c.dom.*;
    import org.xml.sax.InputSource;
    
    public class DomDemo {
    
        public static void main(String[] args) throws Exception{
            String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                       + "<ns1:schema xmlns:ns1='http://example.com'>"
                           + "<ns1:tag1>"
                               + "<ns1:tag2>value</ns1:tag2>"
                           + "</ns1:tag1>"
                       + "</ns1:schema>";
    
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            docBuilderFactory.setNamespaceAware(true);
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(new InputSource(new StringReader(xml))); 
    
            NodeList nl = doc.getElementsByTagNameNS("http://example.com", "tag2");
    
            String a = nl.item(0).getTextContent();
            System.out.println(a);
        }
    
    }
    

    Output

    value
    

    ALTERNATE APPROACH

    You can also use the javax.xml.xpath APIs (included in Java SE 5 and above) to query a value from an XML document. These APIs offer a lot more control than getElementsByTagNameNS.

    XPathDemo

    package forum13166195;
    
    import java.io.StringReader;
    import java.util.Iterator;
    import javax.xml.namespace.NamespaceContext;
    import javax.xml.xpath.*;
    
    import org.xml.sax.InputSource;
    
    public class XPathDemo {
    
        public static void main(String[] args) throws Exception{
            String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                       + "<ns1:schema xmlns:ns1='http://example.com'>"
                           + "<ns1:tag1>"
                               + "<ns1:tag2>value</ns1:tag2>"
                           + "</ns1:tag1>"
                       + "</ns1:schema>";
    
            XPath xpath = XPathFactory.newInstance().newXPath();
            xpath.setNamespaceContext(new NamespaceContext() {
    
                public String getNamespaceURI(String arg0) {
                    if("a".equals(arg0)) {
                        return "http://example.com";
                    }
                    return null;
                }
    
                public String getPrefix(String arg0) {
                    return null;
                }
    
                public Iterator getPrefixes(String arg0) {
                    return null;
                }
    
            });
    
            InputSource inputSource = new InputSource(new StringReader(xml));
            String result = (String) xpath.evaluate("/a:schema/a:tag1/a:tag2", inputSource, XPathConstants.STRING);
            System.out.println(result);
        }
    
    }
    

    Output

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

Sidebar

Related Questions

i want to get the value of this td my code function check() {
I want to get the value of my JFormattedTextField and convert it to string
I want to get a single value(aktiv) out of a table called wartung. include('blog/includes/db_connect.php');
I want to get some value from a string. For example: String string= Name:
Using XPath. I have this doc: <?xml version=1.0?> <root> <items> <item1> <tag1>1</tag1> <tag2>DFGGFDGF</tag2> <tag3>3</tag3>
I want to get one value out of 5 which are the same, lets
So I have an XML file here, and I want to get a value
I want to get value in a date picker and add to textfield_date..I used
I want to get the value from an array thats in a while fetchrow..
I want to get the value of div using webdriver and not Selenium For

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.