• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Removing the XML node from a DOM object

 
Paul Policarp
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

I'm using the w3 DOM API for XML manipulation and need to add a subtree (constructed by using "parse()" on a ByteArrayInputStream derived from a string) to another DOM.

I get a org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.

I think this is because the DOM created with parse() also contains the <?xml version...etc> tag. How can I remove it?

Here's the code sample:

 
Rob Spoor
Sheriff
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Node, which is extended by both Document and Element, has methods getParentNode() and removeChild(Node). So you can detach any Node from its parent as such:
 
Paul Policarp
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, but my XML looks like this:

<?xml version=1.0...>
<snapshot>
<node> value<node>
.
.
.
</snapshot>

I need everything BUT "<?xml version=1.0...>" as a DOM. Can I remove it with removeChild? Does it have a parent node?
 
Rob Spoor
Sheriff
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens if you print out parent?
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Policarp wrote:I get a org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.

I think this is because the DOM created with parse() also contains the <?xml version...etc> tag. How can I remove it?


No, that's a completely wrong guess at the problem. The problem is exactly what the error message says it. To fix the problem, have a look at the Document.importNode method.
 
Paul Policarp
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how I amended the code:



And then inserted your code. This is my output:

*****#document
++++++snapshot

And then I get a NullPointerException on

configNode.appendChild(inlineDoc.getDocumentElement());
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Policarp wrote:Ok, but my XML looks like this:

<?xml version=1.0...>
<snapshot>
<node> value<node>
.
.
.
</snapshot>

I need everything BUT "<?xml version=1.0...>" as a DOM. Can I remove it with removeChild? Does it have a parent node?


No, that's a completely wrong understanding of DOM. Your DOM only contains the nodes of the document. It doesn't "contain" the prolog from the text version of the document; the prolog is produced when you serialize the DOM.

If you don't want your serialized document to contain a prolog, then do something differently in the serialization process to exclude the prolog.
 
Paul Policarp
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I didn't know all these things. Import node worked like a charm once I got the hang of it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic