• 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

help regarding createing node in xml file

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when iam trying to create a node in xml file.it is not updating in the xml file.iam sending my sample program.please tellme what is the problem.iam using dom parser with jaxp api.
import org.w3c.dom.*;
import javax.xml.parsers.*;
public class parse1
{
public static void main(String args[]) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("d://puvvada//book1.xml");
System.out.println("after creatomg xml file");
Node rootNode = document.getDocumentElement();
String s = rootNode.getNodeName();
System.out.println("rootnode=" + s);
NodeList list = document.getElementsByTagName("person");
int length = list.getLength();
System.out.println("length=" + length);
Node temp=null;
Node n1=list.item(1);
Element e=(Element)n1;
System.out.println("hello="+ e.getChildNodes().item(1).getFirstChild().getNodeValue());
System.out.println("la"+e.getChildNodes().item(3).getLastChild().getNodeValue());
System.out.println(e.getChildNodes().item(5).getLastChild().getNodeValue());
Node ndNewColor = document.createElement("color");
ndNewColor.appendChild(document.createTextNode("red"));


catch (Exception e)
{
e.printStackTrace();
}
}
}
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I am not wrong Document object of DOM is memory representation if xml document. It is not wrapper on the physical file. So if you want your changes to be reflected in the file, you need to save the file explicitly.
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes. DOM is a memory representation of the XML tree. If you want to reflect the changes to the xml file, you'll have to use a wrapper. Read this
 
pvsr rao
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as suggested by you i have used domsource and streamresult.but unable to update the xml file.iam sending my sample program.please see where the fault is. iwant to create a node namely color.but unable to create the node
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
public class parse1
{
public static void main(String args[]) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("d:/puvvada/book1.xml");
System.out.println("after creating xml file");
Node rootNode = document.getDocumentElement();
String s = rootNode.getNodeName();
System.out.println("rootnode=" + s);
NodeList list = document.getElementsByTagName("person");
int length = list.getLength();
System.out.println("length=" + length);
Node temp = null;
Node n1 = list.item(1);
Element e = (Element) n1;
System.out.println("hello=" + e.getChildNodes().item(1).getFirstChild().getNodeValue());
System.out.println("la" +e.getChildNodes().item(3).getLastChild().getNodeValue());
System.out.println(e.getChildNodes().item(5).getLastChild().getNodeValue());
Node ndNewColor = document.createElement("color");
ndNewColor.appendChild(document.createTextNode("red"));
NodeList list1 = document.getElementsByTagName("first");
Node n2 = list.item(1);
Element e1 = (Element) n2;
System.out.println(e.getChildNodes().item(1).getFirstChild().getNodeValue());
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new java.io.FileOutputStream( "d:/puvvada/book1.xml"));
transformer.transform(source, result);
}
catch (Exception e) {
e.printStackTrace();
}

}
}
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're creating two new nodes, but you are not actually inserting them into the DOM tree. The Node.insertBefore and Node.appendChild methods would do this.
 
pvsr rao
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as specified by you i have used inserBefore. but it is not creaating nodes.please see the problem in the program.when i execute this program one node is totally deleting.but my purpose is creating a node.please see in my program where the problem is
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
public class parse1
{
public static void main(String args[]) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("d:/puvvada/book1.xml");
System.out.println("after creatomg xml file");
Node rootNode = document.getDocumentElement();
String s = rootNode.getNodeName();
System.out.println("rootnode=" + s);
NodeList list = document.getElementsByTagName("person");
int length = list.getLength();
System.out.println("length=" + length);
Node temp = null;
Node n1 = list.item(0);
Element e = (Element) n1;
System.out.println("hello=" + e.getChildNodes().item(1).getFirstChild().getNodeValue());
System.out.println("la" +e.getChildNodes().item(3).getLastChild().getNodeValue());
System.out.println(e.getChildNodes().item(5).getLastChild().getNodeValue());

NodeList list1 = document.getElementsByTagName("first");
Node n2 = list.item(1);
Element e1 = (Element) n2;
System.out.println(e.getChildNodes().item(1).getFirstChild().getNodeValue());
Node color = document.createElement("color");
color.appendChild(n1);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new FileOutputStream( "d:/puvvada/book1.xml"));
transformer.transform(source, result);
}
catch (Exception e) {
e.printStackTrace();
}

}
}
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Node color = document.createElement("color");
color.appendChild(n1);



Still, you're creating a new element, but you are not adding it to the DOM tree. In addition, if you check the javadoc for appendChild, you'll see that it removes its argument from the tree. So, yes, you are removing one element from the tree, and not adding one.
 
pvsr rao
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i want to create element e12.in the program i have specified it.but it is not updating in the xml file. iam sending my program.please tellme the solution for this.
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
public class parse1
{
public static void main(String args[]) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("d:/puvvada/book1.xml");
System.out.println("after creatomg xml file");
Node rootNode = document.getDocumentElement();
Element el2 = document.createElement("el2");
Text t = document.createTextNode("el2 text");
el2.appendChild(t);
String s = rootNode.getNodeName();
System.out.println("rootnode=" + s);
NodeList list = document.getElementsByTagName("person");
int length = list.getLength();
System.out.println("length=" + length);
Node temp = null;
Node n1 = list.item(0);
Element e = (Element) n1;
System.out.println("hello=" + e.getChildNodes().item(1).getFirstChild().getNodeValue());
System.out.println("la" +e.getChildNodes().item(3).getLastChild().getNodeValue());
System.out.println(e.getChildNodes().item(5).getLastChild().getNodeValue());
System.out.println(e.getTagName());
NodeList list1 = document.getElementsByTagName("first");
Node n2 = list.item(1);
Element e1 = (Element) n2;
System.out.println(e.getChildNodes().item(1).getFirstChild().getNodeValue());
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(rootNode);
StreamResult result = new StreamResult(new FileOutputStream( "d:/puvvada/book1.xml"));
transformer.transform(source, result);
}
catch (Exception e) {
e.printStackTrace();
}

}
}
 
Ranch Hand
Posts: 687
Hibernate jQuery Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


You need to add [appendChild(el2)] el2 back to the node in the document where you want it to be.

The above lines of code will just create the node, it won't add implicit to the document. you will have to do that explicitly.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic