You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@xmlbeans.apache.org by Sebastian Bossung <sb...@jbib.de> on 2005/04/26 18:09:04 UTC

Moving nodes between documents

Hi

I am creating nodes in one document, which I later need to insert into 
another document. I can't create them in the second document right 
away, because at creation time I don't know which document this is 
going to be. Here is some code that reflects the situation (the 
node's class is SW, for brevity): 

----------------------------------------------------
SWDocument doc = SWDocument.Factory.newInstance();
SW sw1 = doc.addNewSW();
sw1.setName("test1");
		
SWDocument doc2 = SWDocument.Factory.newInstance();
SW newS = doc.addNewSW();
newS.setName("newS");
		
sw1.addNewSW();
sw1.setSWArray(sw1.sizeOfSWArray() - 1, newS);

SW newS2 = SWDocument.Factory.newInstance().addNewSW();
newS2.setName("newS2");
newS.addNewSW();
newS.setSWArray(newS.sizeOfSWArray() - 1, newS2);
		
System.out.println(doc.xmlText());
---------------------------------------------------

The last line prints this (reformatted):

<xml-fragment>
	<dok:sw xmlns:dok="...">
		<dok:name>test1</dok:name>
		<dok:sw>
			<dok:name>newS</dok:name>
		</dok:sw>
	</dok:sw>
	<dok:sw xmlns:dok="...">
		<dok:name>newS</dok:name>
		<dok:sw>
			<dok:name>newS2</dok:name>
		</dok:sw>
	</dok:sw>
</xml-fragment>

I would have expected one continuous document with a hierarchy of:
- test1
   - newS
       - newS2
I am not sure that my usage of addNewSW() and setSWArray(...) is as 
intended, but it does not raise any exceptions.

How do I move nodes between documents properly?

Thanks for your help!

Sebastian

-- 
Sebastian Bossung
Software Systems Group (4-02)
Hamburg University of Science and Technology
Harburger Schlossstrasse 20, 21071 Hamburg, Germany
http://www.sts.tu-harburg.de/~se.bossung

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org