10

我想从 tag2 中获取值,我得到了一个 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>"; 

然后将其解析为一个文档,并希望通过 tagnameNS 获取元素。但是当我运行这个时,节点列表是空的,为什么?

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();

仍然不适用于 URI。

4

3 回答 3

20

getElementsByTagNameNS正在返回结果。问题是您当前调用了错误的方法来从结果元素中获取文本内容。你需要打电话getTextContext()而不是getNodeValue()

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

DomDemo

下面是一个完整的代码示例。

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);
    }

}

输出

value

替代方法

您还可以使用javax.xml.xpathAPI(包含在 Java SE 5 及更高版本中)从 XML 文档中查询值。这些 API 提供了比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);
    }

}

输出

value
于 2012-10-31T20:35:40.263 回答
4
 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);

输出

value
于 2017-11-08T01:22:13.710 回答
0

您需要传递命名空间 URL;不是您在 XML 中创建的本地别名。

于 2012-10-31T19:34:03.503 回答