Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

little confusion with appendChild()

 
jeff willis
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • 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).
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!