aspose file tools*
The moose likes XML and Related Technologies and the fly likes Updating XML 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 "Updating XML" Watch "Updating XML" New topic
Author

Updating XML

Randy Miller
Ranch Hand

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

It´s me again with another XML related question ;)
So,basically,I now try to create a dynamic process for replacing XML sections.

Just to give you a short summary: First, I tried to overload a certain section inside a big XML with a smaller XML just containing an equally named section with other data.
Now,I build a GUI around this stuff and read out all possible nodes to be overloaded and save them inside a ComboBox for the user to choose.
When the user picks his node to be replaced, the application should then search for the corresponsing section inside XML number A,copy that one and import it into XML number B.
So far so good.But somehow, the element, I want to import into the other is ALWAYS empty, even though I read out the chosen node correctly(checked this one out with MANY System.out.prints ;) )

The idea is, that,no matter whereever inside the XML the wanted section is located, used to overload the other equally named section inside the second XML,the application is able to locate this section and import it.

Just to illustrate:

XML A:


XML B:


And now,the user gets all possible nodes provided by XML A inside a ComboBox to choose which section he wants to overload. Then, the application uses the name of this section to search inside XML B, retrieves the data and imports it inside XML A. For example the application locates the field "name", after being chosen, inside XML B and overrides the existing data in XML A ("Maier") with the data in XML B("Müller"). This means, the section to be retrieved from XML B doesn´t have to be the root node, but it might be!

Now, here´s my code so far:


You don´t have to look at all the comments I made inside the code ;)
As you can see, I tried a lot of print()-statements to find out, where the problem is.
And apparently, my Element is null??But I use the correct name for the node to locate it.
I just can´t figure out the problem...

Hopefully someone has an idea

Greetings,

Randy
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
When you say the Element is null, are you talking about the test in line 38 or something else>

Bill
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Check the list length for any items present in it first using NodeList:getLength() method.
Randy Miller
Ranch Hand

Joined: Feb 13, 2012
Posts: 44
Hi,

to Bill: Line 28 to be exact!I know,A LOT of comments, I sort of "marked" it in there. Right at this position, the element is zero, even though, the searched node name exists in BOTH XMLs and the name isn´t wrong at all.

to John: Ok, tried that one. It´s zero??Now why´s that??The node exists and the name is correct...Maybe it just won´t find the element??
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
1. Check if the node really present in the document by giving straight getElementsbyTagName("NodeName") and print the NodeList size.

2. Print it and see if this is really equals the node name - chooseSection.getSelectedItem().toString() (which you use in getElementsByTagName() method).
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Look at the JavaDocs for org.w3c.dom.NodeList

You will see that the item( n ) method returns null if the index is not a valid one.

Therefore, your NodeList is empty.

Bill
Randy Miller
Ranch Hand

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

Did EXACTLY what you suggested and here are the results ;)



So,the name is definitely correct.And there also is an existing list.
I just can´t interpret what the output for the list means.

When I use the list.item(index) method and use 0, the output is the section I wanted, BUT if I use any other index it´s null...
Strange...

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
William wrote:
You will see that the item( n ) method returns null if the index is not a valid one.

Why do you feel it's strange? Since the list has one element, hence item(0) returns it. If you try to query any other index in the item() method, it should return null as pointed out in the Java API.
Randy Miller
Ranch Hand

Joined: Feb 13, 2012
Posts: 44
Ok John,now I found THE problem, but I don´t have ANY clue how to solve this.
Let´s look at this code snippet of my replace() method:



If I start the program just like this, the chosenSection in BOTH System.out.prints is perfectly correct. It´s basically a String from a JComboBox where the user can choose his node to be replaced.
The kicker is, the list length in THIS scenario is 0...

If I alter the code snippet and write the actual String as a replacement of chosen Section just like this:



Now,the programm works perfectly fine...Even though it uses the EXACT same String,just now not represented by chosenSection...
But chosenSection holds the EXACT same String...How in the world could this be???
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Not sure why the issue happens. Try using the String's trim() function on your chosenSection for removing any unseen additional spaces.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 507
    
    3
Why don't you post a realistic chosenSection instead of "parameters" one time and "variableParameters" the other time and chooseSection.getSelectedItem().toString() yet another time? Try not do abstraction if you don't know much on dom and what can be abstracted away and what cannot --- don't worry I can grant your being very competant in swing no problem.

Give one example of chooseSection.getSelectedItem().toString(). Does it sound like "person" if your xml A and B are realistic, faithfully reflecting the critical aspect. If the latter is yes and the former is not "person", then you're in trouble.

It is the tag name to be appeared in the getElementsByTagName(), not Maier or Müller or whatever...

Even if you said the combobox for selection have "person" entry, you still have big gap to fill to do a search. If nothing is that realistic and you change presentation all the time, you lead the game in turning round and round no end. Sorry for being so frank talking!
Randy Miller
Ranch Hand

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

John, I can´t thank you enough my friend!!THAT was the problem!!There were stupid whitespaces
The method worked just perfect, but I never saw the whitespaces...
Thank you thank you thank you,you´re Awesome

And g tsuji,thank you for trying to help out as well
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Updating XML