当前位置: 动力学知识库 > 问答 > 编程问答 >

java - How to construct a new document builder out of an existing builders node?

问题描述:

I have the following code:

DocumentBuilderFactory dbFactory_ = DocumentBuilderFactory.newInstance();

Document doc_;

DocumentBuilder dBuilder = dbFactory_.newDocumentBuilder();

StringReader reader = new StringReader(s);

InputSource inputSource = new InputSource(reader);

doc_ = dBuilder.parse(inputSource);

doc_.getDocumentElement().normalize();

and then I traverse doc_ in order to get a specific node. I would then like to create a new dBuilder with that node. What I've been trying so far with little success is to convert my node to a string and then have the dBuilder.parse the string but that has not been working because I'm running into namespace problems and other things.

<Random>

<Fixed></Fixed>

</Random>

So with this I would take the <Fixed> node out and create a completely new class where it is the root node.

网友答案:

Create a new document and then import your node into it, as shown below:

Document otherDoc = dBuilder.newDocument();
Node importedNode = otherDoc.importNode(myNode, true);
otherDoc.appendChild(importedNode);
分享给朋友:
您可能感兴趣的文章:
随机阅读: