Java DOM XML 正在跳过 xmlns 属性

标签 java xml dom xml-namespaces

我需要在 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,我该怎么做才能获取所有信息?

非常感谢计算器

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

最佳答案

摆脱 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 行.一些研究表明,声明特定命名空间的行为是未定义的,因此不推荐。

关于Java DOM XML 正在跳过 xmlns 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1294981/

相关文章:

java - 如何让 Eclipse 将该项目(由 Intellij 制作)识别为 Java 项目?

Java Hibernate将对象保存在一个数据库中的不同模式中

CSS :host alternative for normal dom

javascript - AngularJS 策略防止类的无样式内容闪现

jquery - 使用 jQuery 将一个属性替换为另一个属性

java - 使用 GridLayout 制作 JList

java - Servlet 中的套接字

xml - XSLT 是 Web 框架的好选择?

sql-server - 同一行上具有相同节点的 T-SQL 解析 XML

c# - 从地址获取纬度经度c#