Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the getElementsByTagNameNS empty in java?

Tags:

java

xml

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.

like image 298
user1512895 Avatar asked Oct 31 '12 19:10

user1512895


2 Answers

 Try this :

  DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
  docBuilderFactory.setNamespaceAware(true);
  DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
  Document doc = docBuilder.parse(new InputSource(new StringReader(xml)));      
  NodeList nodeList = doc.getElementsByTagNameNS("*", "tag2");
  String a = nodeList.item(0).getTextContent();
  System.out.println(a);

Output

value
like image 181
Nadhu Avatar answered Sep 28 '22 20:09

Nadhu


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
like image 38
bdoughan Avatar answered Sep 28 '22 20:09

bdoughan