• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

how to use insertBefore method

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
[ June 21, 2004: Message edited by: kesava chaitanya ]
 
kesava chaitanya
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

except Lasse Koskela no body here to help me out;anyways i solved the problem;this forum should improve long way.

bye
chaitanya
 
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 kesava chaitanya:
except Lasse Koskela no body here to help me out;anyways i solved the problem;this forum should improve long way.


Please note that you are as much responsible for improving this forum than the rest of our registered members and moderators are...

I'm glad that you managed to solve your problem
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,


Could you please tel me how did you solve your problem of using insertbefore method;



Thank you
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vidhi,

Please open a new thread for the issue faced. Dont wake the zombies (dead thread).

Are you using JAXP DOM ? is the issue similar to above. Better explain what error you are facing ?
 
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic