• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Removing the XML node from a DOM object

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens if you print out parent?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic