// 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.
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?
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).