【问题标题】:why getNamespaceURI() always return null?为什么 getNamespaceURI() 总是返回 null?
【发布时间】:2015-02-27 04:37:21
【问题描述】:

为什么 getNamespaceURI() 总是返回 null? printNSInfo 方法出了什么问题

public static void main(String[] args) {
    Document input = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(args[0]);
    Element root = input.getDocumentElement();
    printNSInfo(root);
}

private static void printNSInfo(Node node) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        if (node.getNamespaceURI() != null) {
            System.out.println("Element Name:" + node.getNodeName());
            System.out.println("Local Name:" + node.getLocalName());
            System.out.println("Namespace Prefix:" + node.getPrefix());
            System.out.println("Namespace Uri:" + node.getNamespaceURI());
            System.out.println("---------------");
        }
        if (node.hasAttributes()) {
            NamedNodeMap map = node.getAttributes();
            int len = map.getLength();
            for (int i = 0; i < len; i++) {
                Node attr = map.item(i);
                if (attr.getNamespaceURI() != null) {
                    printNSInfo(attr);
                }
            }
        }
        Node child = node.getFirstChild();
        System.out.println(child);
        while (child != null) {
            printNSInfo(child);
            child = child.getNextSibling();
        }
    } else if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
        System.out.println("Attribute Name:" + node.getNodeName());
        System.out.println("Local Name:" + node.getLocalName());
        System.out.println("Namespace Prefix:" + node.getPrefix());
        System.out.println("Namespace Uri:" + node.getNamespaceURI());
        System.out.println("---------------");
    }
}

输入的xml文件是:

<a:NormalExample xmlns:a="http://sonormal.org/" xmlns:b="http://stillnormal.org/">
    <a:AnElement b:anAttribute="text"> </a:AnElement>
</a:NormalExample>

当我在eclipse中调试时,node.getNamespaceURI()总是返回null,我哪里错了?

【问题讨论】:

  • Node.ELEMENT_NODE 有什么类型?

标签: java dom namespaces


【解决方案1】:

this,你需要设置一个标志factory.setNamespaceAware(true),像这样:

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document input = builder.parse(args[0]);

    Element root = input.getDocumentElement();
    printNSInfo(root);

输出是:

Element Name:a:NormalExample
Local Name:NormalExample
Namespace Prefix:a
Namespace Uri:http://sonormal.org/
---------------
...continue...

【讨论】:

  • 哦,就是这样!谢谢。但我还有一个问题,如果 printNSInfo() 方法是这样的:void printNSInfo(Document doc) {...},param 是 Document,我不知道 namespaceaware 是真还是假
  • 你需要用namespaceAware构建文档,DocumentBuilder有一个方法isNamespaceAware,但是Document本身不知道是否支持命名空间。
  • 如果param文档是在没有namespaceAware的情况下构建的,如何获取其命名空间信息?
  • 不能,builder不保留命名空间!
  • public class NamespaceAnalyzer { public NamespaceAnalyzer() { } public Document check(Document srcfile) { Document naReport = null; // TODO: 实现 return naReport; } } 检查方法: 输入:一个格式良好的 XML 文档。输出:使用 namespaceAnalysisReport.dtd 描述的命名空间报告格式的命名空间报告文件,报告如何分析输入文档的命名空间信息?
猜你喜欢
  • 2014-09-12
  • 2019-09-18
  • 2014-09-02
  • 2021-01-10
  • 1970-01-01
  • 2011-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多