Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overwriting a specific XML section

 
Randy Miller
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys!

So,I´m quite new here and been working with XML and XSD for just a few weeks. Now I encountered my first problem.Basically,I have 2 XML Files.One of them is an entire XML-File(let´s call it the basic XML file) which you can actually validate against a XSD. The other XML File shows just a section contained in the basic XML file.My goal is,to,somehow overwrite the specific section of the basic XML file with the section given in the additional XML file.So,how can I do this? I´ve searched quite a while on the internet but no result helped me in the end!

Greetings,

Randy
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randy - welcome to the Ranch

I have not tried it myself - can you check the replaceChild() method replaceChild()

You can also try first removing the node using removeNode() and then adopting the new node from the other document.

To import a Node from other document, you should use adoptNode()(adoptNode or importNode)
 
Randy Miller
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks John

So,you´re thinking of the same idea, I thought of before ;)
But in my book, this would be pretty complicated for example when I use a really complex XML and just want to replace a really small part of the XML.
Basically, my first idea(just like yours) was, to load both the entire XML and the replacement XML, read the replacement XML and check out, which nodes would be affected.
I want to keep this programm pretty simple,so how do I then retrieve all the information out of the replacement XML and replace the right nodes in the entire XML?
Or Am I thinking way too complicated and there´s an easier way to solve this problem?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't think of another way Randy than to load both the XMLs and have them as documents. I have used this replace method with importNode() successfully. May be others can help.

You can also show your code and tell us where you think it's failing / making it complex.
 
Paul Clapham
Sheriff
Posts: 21319
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could quite likely do that with XSLT as well. (But don't ask me to provide an example unless you're going to provide more specific requirements!)

Generally if you want an XSL transform which outputs something only slightly changed from the input, you base your transform on the identity transformation and add in templates which replace specific nodes by something else.
 
Randy Miller
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So John,I´ll try your proposal to load both XMLs as Document and try to replace the matching parts. I´ll see how that one works out ;)

Paul,the general XSLT idea doesn´t sound too bad,but that´s over the top for my requirement cause I don´t really use XSLTs.

But yesterday another idea came to my mind.How about,I have both of my XMLs inside two seperate Strings and then search for the corresponding tags trying to replace everything that´s in between?
Just struck my mind, but I don´t know if that´s really a good idea ;)

Maybe John,you could give me a short dummy, a small snippet on the replacing stuff?

I´ll try my best to get this thing to work ;)
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randy Miller wrote:you could give me a short dummy, a small snippet on the replacing stuff?



Where -
Person.xml


Employee.xml


Result.xml
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randy Miller wrote:I have both of my XMLs inside two seperate Strings and then search for the corresponding tags trying to replace everything that´s in between?

Very bad idea to manipulate XML's as Strings... You have loads of API to process XML I believe.
 
Randy Miller
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys!

Thanks for your amazing help so far

That´s just what I thought: Working with Strings to manipulate XMLs is quite a bad idea ;)

Thanks for the code snippet
I´ll try it in just a few minutes
Hopefully it works

But so far,here´s something I tried and I´m not sure why that wouldn´t work.


And the parseXML method:


Does anyone see,why this wouldn´t do the trick?Doesn´t look that wrong,right? ;)

Oh yeah,just to give you a short view to the XMLs.


That´s the ENTIRE replacement XML,I want to insert in another XML.Note,that no header is defined.(Shouldn´t be a problem!?)

And here´s a snippet of the XML needing to be altered:


Here,you have a complete header(not shown here,but believe me,it´s there ;) )
And now,the goal is,just to copy the replacement XML into the big XML.But,the needed section is inside the big XML,not directly at the beginning, which means,you need to search for it ;)
After you found the section,replace the old section with the new one.

Now,I´ll try your provided code,hopefully it works
I think,the problem should be quite clear now.But your provided solution looks quite good ;)
Let´s see,whether or not it does the trick for me ;)

 
Randy Miller
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So,just as promised,my update ;)

John,I tried to implement the code snippet you provided,but somehow,it just doesn´t work...
I looked at the XML-files before and after running the application,nothing really changed!?

Maybe I just implemented that bad boy wrong ;)
Take A Look at this:



Maybe I should try to save the altered file in a new XML?Or does anyone know the trick?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try saving the altered document to a new XML file.

In case you need the part of code to write document to a file -


And remember to use print statements to get the flow of the code and check if the document is really being modified...

update - I ran the code you provided and it worked successfully for me
 
Randy Miller
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John,

Really??The code worked for you?Now how´s that possible??
Somehow,for me,not...
The application doesn´t even wanna save the new file...
I entered your code sample inside a try-catch-block right after the last if-statement,just like this:



Thanks for the tip,using System.out.print()-commands ;)
Normally,my program is full of those,but I just threw them out to provide a better view of the code ;)
Did you use EXACTLY the same code,I posted?Or did you change one or two things to make it work?And which one?I posted two examples ;)
Or maybe,the replacement-function isn´t handy for larger XML-Files where I want to replace a section deep inside the XML?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My very first guess would be nodeToBeReplaced might be null. There's where print statements will help you.
 
Randy Miller
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ha,THERE it is ;)
nodeToBeReplaced: nullnullnull...
Good Guess my friend ;)
Now,how´s that possible??I
Looking at the code,I realize,nodeToBeReplaced hasn´t been used,just initialized as null...
Only childNodes is properly use...(I guess ;) )
Therefore,the application correctly terminates.Hm,but how did you get it to work using the code I posted?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to read your code again & again and tell us what is the reason it's stand set as null. Print all childNode's node names prior to this statement.

Again - use print statements to trace the issue.
 
Randy Miller
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So,I´ve tried the print-statements to trace the issue and basically,this is how the outprint looks like:



So basically,the application finds the correct nodes(seemingly),but first,there´s a null??And the value is also null??
Now why´s that?Maybe a formatting problem inside the replacement XML??Maybe unknown spaces?
Or perhaps,the XML just lacks a normal XML-Header?

Of course,this is just wild guessing,trying to find an answer ;)
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's obvious that there is no variableParameters child node which you are looking for. I think you should still ShowSomeEffort to analyze and debug the code.
 
Randy Miller
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course,there IS a node "variableParameters" John ;)
I tried this 2 different ways,like I posted here and still, I haven´t been able to solve this problem (even though I´m pretty patient) ;)
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randy

Might I suggest you refactor lines such as



to split method calls over multiple lines.
Reducing chained calls makes your code more readable and easier to debug which may help you out in your current predicament.
 
Paul Clapham
Sheriff
Posts: 21319
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randy Miller wrote:Of course,there IS a node "variableParameters" John ;)


Just not where you're looking. Your original code shows that you're looking for elements with that name which are children of the document node. So that would mean that the actual node you're looking for is not a child of the document node.
 
g tsuji
Ranch Hand
Posts: 669
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I refer to the posting of "Wednesday, February 15, 2012 9:09:21 PM".
This is how the specific block of code should look like to assure general applicability.

It conveys at least the precise functioning of replaceChild() and how to find the desired element anywhere as long as we know the NCName (figured as "variableParameters") or the name with a default namespace attached to it.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is nice one Tsuji... Thanks
 
Randy Miller
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys!

So,after sooo many hours of debugging, I finally got it to work

Here´s the winner


The trick is pretty easy or no,better: It was a really stupid error...I got a malformed replacement XML and so therefore,the algorithm just wouldn´t work...
So,I opened the XMLs seperately via Firefox and there was the problem,I was looking for for so long

Thanks again for all your help,you´re awesome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic