Java XML DOMが飛びxmlns性
-
18-09-2019 - |
質問
Iを生成しなければならないxmlファイルをJavaになるようにしている利用DOM(までもokです)、こちらのルートタグのかを
<?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:た場合NAMESPACE_ERRんよ"http://www.w3.org/2000/xmlns/"のNamespaceURI分野)
解決
二つについては取得しなければならない消 xmlns=""
の作成 Document
希望する名前空間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は黙って落とします。見線173の NamespaceMappings
.研究のビットがその行動を宣言する特定の名前空間は未定義があるため、お勧めできません。
他のヒント
DOM名前空間を認識させるために、setNamespaceAware
メソッドを使用してのDocumentBuilderFactoryでそれを有効にすることを忘れないでください。
所属していません StackOverflow