• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Update XML, insert an element.

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am trying add an element into an XML file. At the bottom is my input XML and desired output.
Input#1 is the original XML file.
Output#1 is my desired output.

Here's my code and it throws error at line [120]



ERROR



Input XML


Desired output Basically I want to append the col7 nodes for each <entry> elements.
 
Sheriff
Posts: 27235
87
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
In the XML you posted, an <entries> element has seven children. There are three <entry> elements and four white-space text elements; you may not have been aware of the latter. It looks to me like your code tries to add the new <entry> element as a child of the last of those seven elements, which is whitespace text. Text elements can't have children.

You need to add the new <entry> element as a new child of the <entries> element. You might also want to add another whitespace text element to make the resulting XML look nice, but don't do that until you are told to do so, as it doesn't change the meaning of the document.
 
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[1] Line #21


[2] Lines #23-32

The loop j may be better used by reversing order of the index and coupled it with "break" for the first match. But this is only a detail you have to consider rewriting everywhere.

[3] The main error asked. It is an misunderstanding of appendChild(). Line #48


[4] On the whole, the code does not give enough credit to java's DOM model. If you use DOM to flatten the xml tree using getChildNodes(), it is far away to use the more pertinent method getElementsByTagName() on the child Element everywhere. For instance, line #35

The coding inside would have to be rewritten. It would be greatly simplified and more focused.., eliminating a lot of checking...
 
reply
    Bookmark Topic Watch Topic
  • New Topic