File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Problem Overwriting XML Section Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Problem Overwriting XML Section" Watch "Problem Overwriting XML Section" New topic
Author

Problem Overwriting XML Section

Randy Miller
Ranch Hand

Joined: Feb 13, 2012
Posts: 44
Hi guys!

Some time ago,I tried to find a solution for overwriting a special section inside a big XML with another smaller XML.
Now,I tried and tried,but somehow,my code just won´t work.It doesn´t replace the XML section,it only appends the small XML at the end of the big XML.
Here´s my code:



Does anyone see the problem??I mean,the XMLs are both wellformed and I just can´t see why my program just appends the XML section to the big XML instead of replacing the section with the same name as the replacing section xml inside the big XML.

I hope,you can help me!

Greetings,

Randy
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18989
    
    8

From the API documentation for appendChild():

Adds the node newChild to the end of the list of children of this node.


If you want to replace a node, I suggest you consider a method which has "replace" in its name. Check the API documentation for that one.
Randy Miller
Ranch Hand

Joined: Feb 13, 2012
Posts: 44
Ah,ok,now that explains why the node is appended at the end of the file.
So,I tried to get it to work with the replaceChild() method,but still it won´t work. Just won´t replace it and the program doesn´t really wanna jump into my saving-method(Transformer stuff^^)
Here´s my next try:


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18989
    
    8

Well, I have to say that I don't understand your code. Based on what you said earlier and based on some of the variable names, I have a vague feeling that maybe you should be trying to replace one of the nodes of doc2 by the contents of doc1, rather than what you're doing now. But calling the documents "doc1" and "doc2" is quite unhelpful.
Randy Miller
Ranch Hand

Joined: Feb 13, 2012
Posts: 44
Hi Paul!

Sorry for the unclear code ;)
I tried to name the deciding elements a bit differently, so it´s easier to understand.And I also put the saving mechanism into a new function.



But somehow,still I just won´t get it to work correctly.The save-operation won´t even be triggered and I haven´t figured out yet,if the overwriting mechanism works...
Maybe,now,it´s easier to see the problem for one of you?
Meanwhile,should I find the solution,I´ll post it right here
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18989
    
    8

Okay. So line 12 says you start with "replacementDoc". At line 13 you get all of the children of that document's document node. If one of those children has a node name which matches what you're looking for (line 19) then you attempt to replace that child by this expression:



Right so far?

Well, first of all there is no point in importing into the entireDoc document, since that isn't the one you're working with. You're working with replacementDoc (see line 12). So all you need there is this:



But now this can't work because you're trying to move the document element to be a child of itself.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem Overwriting XML Section