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"));
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.
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.
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: 35437
9
posted
0
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
posted
0
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(); }