How to append xml nodes (as a string) into an existing XML Element node (only using java builtins)?

感情迁移 提交于 2019-12-05 14:51:02

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);

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

Mohamed Habib

I think your question is like this question and there is answer on it : Java: How to read and write xml files?

OR see this link http://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!