Java DOM XML正在跳过xmlns属性 [英] Java DOM XML is skipping xmlns properties

查看:112
本文介绍了Java DOM XML正在跳过xmlns属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 <?xml version =1.0encoding =utf-8?> 
< KeyContainer Version =1.0xmlns =urn:ietf:params:xml:ns:keyprov:pskc:1.0xmlns: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); 
文档xmldoc = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
DOMImplementation impl = builder.getDOMImplementation();
元素e = null;
Node n = null;
xmldoc = impl.createDocument(null,KeyContainer,null);
/ * Noeuds非bouclés* /
元素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);
/ *非相关信息* /
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.0encoding =utf-8standalone =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:有NAMESPACE_ERR,除了 http://www.w3.org/2000/xmlns/ 在NamespaceURI字段中)

解决方案

需要两件事才能摆脱 xmlns =



创建具有所需命名空间URI的文档

  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 。一些研究发现,声明特定命名空间的行为未定义,不推荐。


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

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

Thanks a lot stackoverflow

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

解决方案

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

Create the Document with the desired namespace URI as such:

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:xml attribute, 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.

这篇关于Java DOM XML正在跳过xmlns属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆