且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将xml节点(作为字符串)附加到现有的XML Element节点(仅使用java内置函数)?

更新时间:2021-12-05 00:40:27

你可以在java - 你应该能够得到Rhino等价物:

You can do this in java - you should be able to derive the Rhino equivalent:

DocumentBuilderFactory dbFactory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.newDocument();
Element el = doc.createElement('test');
doc.appendChild(el);


String xml = "<foo bar=\"1\">Hi <baz>there</baz></foo>";
Document doc2 = builder.parse(new ByteArrayInputStream(xml.getBytes()));

Node node = doc.importNode(doc2.getDocumentElement(), true);
el.appendChild(node);

由于 doc doc2 是两个不同的文档,诀窍是将节点从一个文档导入到另一个文档,这是通过 importNode api高于

Since doc and doc2 are two different Documents the trick is to import the node from one document to another, which is done with the importNode api above