• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

little confusion with appendChild()

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my XML file: (sort of)
<ledger>
<transaction>
<data> DATA1 </data>
</transaction>

<transaction>
<data> DATA2 </data>
</transaction>

</ledger>

My application needs to append another TRANSACTION ELEMENT after the DATA2 transaction element. However, my output ends up looking like this:

<ledger>
<transaction>
<data> DATA1 </data>
</transaction>

<transaction>
<data> DATA2 </data>
<TRANSACTION><DATA>data3</DATA></TRANSACTION>
</transaction>

</ledger>


??? How did it end up there?

Here is a code sample:

// get the root, then get all the transactions
// then get the last transaction...
Element root = document.getDocumentElement();
NodeList nl = root.getElementsByTagName("transaction");
Node lastTransaction = nl.item( (nl.getLength()-1) );

// create a new element...
Element descElement = createXMLElement( "DATA",
txtData.getText());

// create another new element for the new transaction record...
Element newTransaction = document.createElement("TRANSACTION");


// place the new "data" with the new transaction...
newTransaction.appendChild(descElement);

// finally, place the new transaction in the document
lastTransaction.appendChild(newTransaction);


...and the new transaction ends up in a place where I didn't expect it.
I'm using the javax.xml.transform class to print out my document to screen to verify that the insert took place.

So...basically I need to know what I'm doing wrong. I need to be able to append new TRANSACTION element to my document.
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This line:

does not append newTransaction after lastTransaction. It appends it as lastTransaction's child as the method name, appendChild(), implies.
 
jeff willis
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, I guess I'm seeing that for myself now. It's still confusing though because if I execute this line of code:

System.out.println("lastTransaction = " + lastTransaction);

The output shows the entire last transaction (i.e. transaction #2). By that I mean all the elements and text between the <transaction> </transaction> tags show up in the output.

By looking at that output, I figured that I was where I needed to be inside the document.


Any idea how I would add another TRANSACTION to my document?
 
jeff willis
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it looks as if I've stumbled onto my own solution here...

I commented out my original appendChild() call and replaced it with an appendChild() call from the root of the document

My code now looks like this:



It works, but I'm wondering if this how the method is supposed to be called in this situation???
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by jw wvu:

It works, but I'm wondering if this how the method is supposed to be called in this situation???


Yes. Before that statement, the root element (<ledger>...</ledger> contains one child (lastTransaction). After executing that statement, it contains two (lastTransaction and newTransaction).
 
passwords must contain 14 characters, a number, punctuation, a small bird, a bit of cheese and a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic