【问题标题】:Java DOM XML is skipping xmlns propertiesJava DOM XML 正在跳过 xmlns 属性
【发布时间】:2010-11-20 16:00:11
【问题描述】:

我需要在 Java 中生成一个 xml 文件,所以我选择使用 DOM(直到一切正常),这是我需要创建的根标记

<?xml version="1.0" encoding="utf-8"?>
<KeyContainer Version="1.0" xmlns="urn:ietf:params:xml:ns:keyprov:pskc:1.0" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" xmlns:xml="http://www.w3.org/XML/1998/namespace">

这是我的源代码

PrintWriter out = new PrintWriter(path);
Document xmldoc = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        DOMImplementation impl = builder.getDOMImplementation();
        Element e = null;
        Node n = null;
        xmldoc = impl.createDocument(null, "KeyContainer", null);
        /* Noeuds non bouclés */
        Element keycontainer = xmldoc.getDocumentElement();
            keycontainer.setAttributeNS(null, "Version", "1.0");
            keycontainer.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:ds","http://www.w3.org/2000/09/xmldsig#");
            keycontainer.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xenc", "http://www.w3.org/2001/04/xmlenc#");
            keycontainer.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xml", "http://www.w3.org/XML/1998/namespace");
            keycontainer.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "urn:ietf:params:xml:ns:keyprov:pskc:1.0");
/* Non relevant Info*/
DOMSource domSource = new DOMSource(xmldoc);
        StreamResult streamResult = new StreamResult(out);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer serializer = tf.newTransformer();
        serializer.setOutputProperty(OutputKeys.ENCODING,"utf-8");
        serializer.setOutputProperty(OutputKeys.VERSION,"1.0");
        serializer.setOutputProperty(OutputKeys.INDENT,"yes");
        serializer.setOutputProperty(OutputKeys.STANDALONE,"yes");
        serializer.transform(domSource, streamResult); 

这就是我得到的

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<KeyContainer xmlns="" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" Version="1.0">

问题是 xmlns 属性为空,缺少 xmlns:xml,如何获取所有信息?

非常感谢stackoverflow

(PS:如果 NamespaceURI 字段中的“http://www.w3.org/2000/xmlns/”以外的任何内容,则得到 NAMESPACE_ERR)

【问题讨论】:

    标签: java xml dom xml-namespaces


    【解决方案1】:

    摆脱xmlns=""需要两件事

    使用所需的命名空间 URI 创建 Document,如下所示:

    xmldoc = impl.createDocument("urn:ietf:params:xml:ns:keyprov:pskc:1.0", "KeyContainer", null);
    

    删除以下行,因为它现在是不必要的:

    keycontainer.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "urn:ietf:params:xml:ns:keyprov:pskc:1.0");
    

    关于xmlns:xml 属性,API 正在默默地删除它。请参阅NamespaceMappings 的第 173 行。一些研究表明,声明特定命名空间的行为是未定义的,不推荐使用。

    【讨论】:

    • 谢谢,有没有办法不在每个子节点中获取 xmlns="" ?
    • 你是如何创建和添加它们的?
    【解决方案2】:

    要让 DOM 命名空间感知,不要忘记在 documentbuilderfactory 中使用 setNamespaceAware 方法启用它。

    【讨论】:

    • 命名空间感知只影响现有文档的解析方式(使用DocumentBuilder定义的parse方法)。它对以编程方式创建的 DOM 节点(这是 OP 所做的)没有任何影响。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-27
    • 2022-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多