• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Wrong Document Error when adding deep copy node to new document (Resolved)

 
Ranch Hand
Posts: 99
Postgres Database Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am getting a wrong document error that I have tried a number of ways to solve. I'd very much appreciate any help you can give.



The error is thrown at line 90. This code has gone through many iterations before getting to here as I've tried to solve the problem many different ways. The reason for the System.out.println was so that I could see how many records xpath found. I discovered it found 3. Thinking I was saving time, the first time around, I tried:



Then I skipped all the Element creation and NodeList iteration. Of course, there are three nodes and I got back only in my String representation when I tried that. Since then I'm trying to add each node (and all its children) to an empty Document. If the code does not make it clear, my purpose here is to Transfomer a Document into a well-formed String representation of the paired down XML Document created from the starting XML Document. Document A has n numbers of parent Nodes and Document B will have n - (Nodes where some child Node != some node_name and some child node != some node_value). Maybe there's an easier way to go about all this?

Appreciate any help.

Regards,
Al
 
Sheriff
Posts: 28328
96
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
I would just use the Document's importNode() method to get nodes which can be appended to the document.
 
Al Johnston
Ranch Hand
Posts: 99
Postgres Database Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul.

I had already tried that as one of the many iterations I went through to get this to work. I would get either a Wrong Document error or a Hierarchical Error depending on what I did after the document.import(node, true) method. That's IF I then directly added the node as a child. Otherwise, I'd get no errors but the document would be empty. So, to fix it all, I had to do this (for anyone else who may have a problem with this in the future):



Not to complain or anything, but the API and what it indicates it will do in this case is not very precise. There seem to be many ways to import and add Nodes to Documents but it's not as simple as just saying "import" and having the Node be where the API says the node will be. First of all, if you don't have a root node in the Document, you'll get an error (I did not want a root node but now I have one). Then, if you have a root node and try to import it and add it to the document by saying:



Throws same error as:



You'll get an error stating that the node is being used in a document other than the one it was created in. Huh? I deep copied it!

The only way I could get it to work is to add the nodes to Elements as children of the Elements and then add the Element (with at least one root value) to the document. There is already a lot of noise in this code. It would be great if the API were simpler and also worked in this case.

Thanks again for your help.

Best,
Al
 
Paul Clapham
Sheriff
Posts: 28328
96
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

Al Johnston wrote:but it's not as simple as just saying "import" and having the Node be where the API says the node will be.


Here's all the API has to say about where the imported node will be:

The returned node has no parent


All that "importNode" does is to make the node belong to the Document. It doesn't insert it anywhere in the document. You have to do that after you import it.

First of all, if you don't have a root node in the Document, you'll get an error (I did not want a root node but now I have one).


Every XML document must have a root element. So if you want an XML document, you have to start by creating a root element. You don't have a choice about that.
 
Al Johnston
Ranch Hand
Posts: 99
Postgres Database Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul. That makes sense and it has been a long time since I worked with XML in any material way as almost everything here is JSON. But, the gist of my concern was around the following: Do you know why (doc.importNode OR node.clone) in conjunction with doc.appendChild throws a wrong document error? Why must I create an Element to do all this and then append the Element to the doc?
 
Paul Clapham
Sheriff
Posts: 28328
96
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
No, sorry, I don't really know. I do as little work with the W3C DOM as possible; if there's another way to do what I want, I'm going to choose that other way.
 
Surfs up space ponies, I'm making gravy without this lumpy, tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic