Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to use insertBefore moethod

843834Jun 21 2004 — edited Jun 22 2004
hi

i am able to create xml document with the below code;whenever i add new one it will append after last node usually;if i want to add new node after first node what changes should i made to my code;i heard about insertBefore;how to use that method;i have seen javaalamanac examples but i am unalbe to understand how to apply to my code;


<India>
<IndianPlayer PlayerId="57">
<Name>ramu</Name>
<Age>46</Age>
</IndianPlayer>
<IndianPlayer PlayerId="24">
<Name>fsfs</Name>
<Age>24</Age>
</IndianPlayer>
</India>


// i want to append like this after first one

India>
<IndianPlayer PlayerId="57">
<Name>ramu</Name>
<Age>46</Age>
</IndianPlayer>

<---i want to insert new node after 1st one.....>
<IndianPlayer PlayerId="157">
<Name>venkat</Name>
<Age>46</Age>
</IndianPlayer>


<IndianPlayer PlayerId="24">
<Name>fsfs</Name>
<Age>24</Age>
</IndianPlayer>

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.*;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.*;

DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
DocumentBuilder db = fac.newDocumentBuilder();
doc = db.parse(new FileInputStream("name.xml"));
root = doc.getDocumentElement();

NodeList nl= root.getElementsByTagName("IndianPlayer");



Element main1 = doc.createElement("IndianPlayer");
main1.setAttribute("PlayerId",request.getParameter("playerid2"));



Element productnames1 = doc.createElement("Name");
Text product1 = doc.createTextNode(request.getParameter("name1"));


Element price1 = doc.createElement("Age");
Text priceValues1 = doc.createTextNode(request.getParameter("age1"));



productnames1.appendChild(product1);
price1.appendChild(priceValues1);



main1.appendChild(productnames1);
main1.appendChild(price1);

root.appendChild(main1);
saveDocAsFile(doc,fname);


bye
chaitanya
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 20 2004
Added on Jun 21 2004
1 comment
211 views