Java DOM XML 正在跳过 xmlns 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1294981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:00:09  来源:igfitidea点击:

Java DOM XML is skipping xmlns properties

javaxmldomxml-namespaces

提问by Lliane

I need to generate an xml file in Java, so I chose to use DOM (until there everything is ok), here is the root tag of what i need to create

我需要用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">

Here is my source code

这是我的源代码

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

And here is what I get

这就是我得到的

<?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">

Problem is the xmlns property is empty, and xmlns:xml is missing, what can I do to get all information ?

问题是 xmlns 属性为空,并且缺少 xmlns:xml,我该怎么做才能获取所有信息?

Thanks a lot stackoverflow

非常感谢 stackoverflow

(PS : Got NAMESPACE_ERR if anything else than "http://www.w3.org/2000/xmlns/" in NamespaceURI field)

(PS:如果NamespaceURI 字段中除了“ http://www.w3.org/2000/xmlns/”之外还有其他内容,则会得到 NAMESPACE_ERR )

采纳答案by laz

Two things are required to get rid of xmlns=""

需要两件事才能摆脱 xmlns=""

Create the Documentwith the desired namespace URI as such:

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

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

Remove the following line as it is now unnecessary:

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

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

Regarding the xmlns:xmlattribute, the API is silently dropping it. See line 173 of NamespaceMappings. A bit of research turns up that the behavior of declaring that particular namespace is undefined and is not recommended.

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

回答by Paul de Vrieze

To make DOM namespace aware, do not forget to enable it in the documentbuilderfactory using the setNamespaceAwaremethod.

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