File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes help regarding createing node in xml file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "help regarding createing node in xml file" Watch "help regarding createing node in xml file" New topic
Author

help regarding createing node in xml file

pvsr rao
Ranch Hand

Joined: Oct 05, 2005
Posts: 102
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();
}
}
}
Onkar Shendge
Greenhorn

Joined: May 29, 2002
Posts: 11
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.


- Onkar
Rahul Kakkar
Ranch Hand

Joined: Apr 17, 2005
Posts: 70
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

Joined: Oct 05, 2005
Posts: 102
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();
}

}
}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42951
    
  72
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

Joined: Oct 05, 2005
Posts: 102
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
Marshal

Joined: Mar 22, 2005
Posts: 42951
    
  72
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

Joined: Oct 05, 2005
Posts: 102
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();
}

}
}
Devesh H Rao
Ranch Hand

Joined: Feb 09, 2002
Posts: 687



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.
 
Consider Paul's rocket mass heater.
 
subject: help regarding createing node in xml file