0

DOM APi を使用して XML ドキュメントを作成しようとしていますが、次のコードを使用すると、期待どおりの結果が得られました

Element rootTreeNode = document.createElementNS("http://schemas.microsoft.com/ado/2007","ex" + ":Ex")

これは、出力コンソールのタグ付きの出力です

 ex:Ex Version="1.0" xmlns:ex="http://schemas.microsoft.com/ado/2007"/

今、この要素に次を追加したい

**xmlns:gp**="http://www.pst.com/Protocols/Data/Generic"

そして、私はxmlns:gpで成功し ません。次のようなものを使用しようとしました

rootTreeNode.setAttributeNS("xmlns" ,"gp","http://www.pst.com/Protocols/Data/Generic")

そして、私は次のようにそれを手に入れました

**xmlns:ns0="xmlns"** **ns0:gp**="http://www.pst.com/Protocols/Data/Generic"

そして、最初のパラメーターに null を入れた場合

rootTreeNode.setAttributeNS(null ,"gp","http://www.pst.com/Protocols/Data/Generic")

xmlns なしのURLgpだけを取得します。

ここで何が間違っていますか?

ありがとう!!!

4

1 に答える 1

1

完全なテスト:

DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();

Document doc = docBuilder.newDocument();

Element root = doc.createElementNS("http://schemas.microsoft.com/ado/2007","ex" + ":Ex");
root.setAttributeNS("http://www.w3.org/2000/xmlns/" ,"xmlns:gp","http://www.pst.com/Protocols/Data/Generic");

doc.appendChild(root);

TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes");

StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
trans.transform(source, result);
String xmlString = sw.toString();

System.out.println("Xml:\n\n" + xmlString);
于 2012-10-17T13:19:50.293 に答える