为什么getNamespaceURI()总是返回null?

5
为什么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是什么类型? - Kick Buttowski
1个回答

8

这里可以看到,您需要设置一个标志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) {...},参数是一个Document,而我不知道namespaceaware是true还是false。 - Marshal
你需要使用 namespaceAware 构建文档,DocumentBuilder 有一个方法 isNamespaceAware,但是 Document 本身无法知道是否具有命名空间感知能力。 - Arturo Volpe
如果参数document是在未启用namespaceAware的情况下构建的,如何获取其命名空间信息? - Marshal
你不行,构建器不保留命名空间! - Arturo Volpe
公共类NamespaceAnalyzer { public NamespaceAnalyzer() { } public Document check(Document srcfile) { Document naReport = null; // TODO: 实现
return naReport; } } check方法: 输入:格式良好的XML文档。 输出:使用namespaceAnalysisReport.dtd描述的命名空间报告格式生成命名空间报告文件,报告如何分析输入文档的命名空间信息?
- Marshal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接