Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Xml append node problem

Have Programming Question? Ask it here!
 
 
 


Ritesh Kumar
Xml append node problem
1 Answer(s)      a year and 11 months ago
Posted in : Java Beginners


print("code sample");Question:
I create a XML which looks like this:
              <cwMin>31</cwMin>
         The code used in generating this XML is :
              XMLOutputFactory xof = XMLOutputFactory.newInstance();
              XMLStreamWriter xtw = null;
              StringWriter stringWriter = new        StringWriter();
              xtw = xof.createXMLStreamWriter(stringWriter);
              xtw.writeStartDocument();
              xtw.writeStartElement("cwMin");
              xtw.writeCharacters("31");
              xtw.writeEndElement();
              xtw.writeEndDocument();
              xtw.flush();
              xtw.close();
              System.out.println("111 ---> "+stringWriter.toString());
              Node nds = parse(stringWriter.toString()); //Parse is a custom method to convert String to Node.

Output:

111 --->  <?xml version="1.0" ?><cwMin>31</cwMin>

org.w3c.dom.DOMException: WRONGDOCUMENTERR: A node is used in a different document than the one that created it.

Now that, I already have a node "nd" which is of type org.w3c.dom.Node.
and the value of "nd" is :
<?xml version="1.0" ?>
<wmmApResp>
<wlanId>0</wlanId>
<profileId>2</profileId>
<aifs>6</aifs>
</wmmApResp>

This node "nd" is available in the code above. When I try to append nd to nds using nd.appendChild(nds), I have the following exception: org.w3c.dom.DOMException: WRONGDOCUMENTERR: A node is used in a different document than the one that created it.

How do I rectify/solve this?

Advertisement
View Answers

September 7, 2012 at 3:28 PM


Here is an example that appends a new node to the existing xml document. Here is an xml file where we are going to append a new node.

<?xml version="1.0" encoding="UTF-8" standalone="no"?><school>
<student>
<name>Angelina</name>
<address>Delhi</address>
</student>
<student>
<name>Martina</name>
<address>Mumbai</address>
</student>
</school>

Here is a code that appends a new node student with name and address elements.

import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

public class AppendNode {

    public static void main(String[] args) {
        try {
            File xmlFile = new File("C:\\file.xml");
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            Document document = documentBuilder.parse(xmlFile);
            Element documentElement = document.getDocumentElement();
            Element textNode = document.createElement("name");
            textNode.setTextContent("Rose");
            Element textNode1 = document.createElement("address");
            textNode1.setTextContent("Delhi");
            Element nodeElement = document.createElement("student");
            nodeElement.appendChild(textNode);
             nodeElement.appendChild(textNode1);
            documentElement.appendChild(nodeElement);
            document.replaceChild(documentElement, documentElement);
            Transformer tFormer =
                    TransformerFactory.newInstance().newTransformer();
            tFormer.setOutputProperty(OutputKeys.METHOD, "xml");
            Source source = new DOMSource(document);
            Result result = new StreamResult(xmlFile);
            tFormer.transform(source, result);


        } catch (Exception ex) {
            System.out.println(ex);
        }
    }
}


Related Tutorials/Questions & Answers:
Xml append node problem
Xml append node problem   print("code sample");Question: I create... to append a new node. <?xml version="1.0" encoding="UTF-8" standalone="no"?><... to Node. Output: 111 ---> <?xml version="1.0" ?><cwMin>31<
Java append new node to XML file
Java append new node to XML file In this tutorial, you will learn how to append new node to xml file. Sometimes there is a need to append a new XML node to the xml file. Here is an xml file where we are going to append a new node
Advertisements
xml_append - XML
xml_append  hi i have a xml file im.xml its contents: asaddasahellojim i have to add more tags to .. pls help me to solve this.. ...://www.roseindia.net/xml/ Thanks
i want to remove specific node in xml file
i want to remove specific node in xml file   <A> <B>hi <C>by <A> <B>hellow <C>how r u ? i want to delet node which is == hellow using java program please help me . tanks in advance
Java--Xml Modify Node Value - Java Beginners
Java--Xml Modify Node Value  I have the following xml. test..., Updating xml node is not sufficient to update that xml. You need to prepare whole xml again after updating the node. Use the following method to update xml
Java Xml -Node retrieval - Java Beginners
Java Xml -Node retrieval  I have the following xml test_final_1 2009-025T13:23:45 B2B In the above xml,I want to retrieve... = DocumentBuilderFactory.newInstance(); File xmlFile2 = new File("D:\\XML_Utility\\Version 11
XML load problem - XML
XML load problem  I have load the xml document in javascript.getting..., Plz give full details with source code to solve the problem and visit to : http://www.roseindia.net/xml/dom/ Thanks    New Document
java program to create xml file with append data in well-formed
java program to create xml file with append data in well-formed   Sorry sir your given xml append data program is not well-formed. I'll make you more clear what I want. If this is my xml file Tom Cruise 45 Now when I append
Java-Xml -Modify and Update Node - Java Beginners
Java-Xml -Modify and Update Node   test_final_1 2009-025T13:23...(); System.out.println("XML Data: "); DefaultHandler dHandler = new...){ System.out.println("XML File hasn't any elements"); e.printStackTrace
xml problem - XML
xml problem  URGENT>>>>> how to retrieve data from hash tables without a JDBC connection?????? Thanks & Regards SAIBHARGAV146
java and xml problem - XML
java and xml problem  hi, i need to write a java program that generates an xml file as follows: aaa vvv... XML file: "); //String str = bf.readLine(); int no = Integer.parseInt("1
Java code to append/add data into a existing xml file
Java code to append/add data into a existing xml file  Java code to append data into a existing xml file, As user enters data it overwrites the previous XML file,I want it to be append the data in XML file rather then overwriting
how to append data to XML file in proper format using JSP
how to append data to XML file in proper format using JSP  hello i was appending my XML file to add more data entered by user in JSP page.But...;/html> XMl file creates successfully at first instance but unable to append
Problem with external DTD - XML
Problem with external DTD  Hi, This class generate an XML file.But while I am running that generated xml file its giving error as can not locate... classes. OutputFormat of = new OutputFormat("XML","ISO-8859-1",true); of.setIndent
jasper problem - XML
jasper problem  URGENT PROBLEM!!!!!!!!!!!!! how can we use a hash table in jasper reports to generate reports without using database connection directly but access data from the hash table what we needed for creation
DOM Example For Creating a Child Node 
;) and append this child with the root node. In this last step, we create... about how to add a Child Node in Dom document. The addition of child node is a very logical. To add a child node first we create blank DOM document and then we
Problem facing in SAX Parsing - XML
Problem facing in SAX Parsing  I have facing the issue in SAX Parsing like i have got the xml and the xml structure is like below... can find the above xml there is no CountryCode in the second record and 4th
Problem facing in SAX Parsing - XML
Problem facing in SAX Parsing  I have facing the issue in SAX Parsing like i have got the xml and the xml structure is like below... 0668420957700159USD you can find the above xml
concat and append
and append?   hiii, Concat is used to add a string at the end of another string.But append() is used with String Buffer to append character sequence... is created.But in case of StrinBuffer APPEND() that is not the case
Java get Node Text
of the node in the specified XML file. For this we have create a 'employee.xml' file...;employee.xml".In order to get the text of the node specified in a XML file, we... of GetNodeText.java you get the text of the node specified in "employee.xml" xml
Node class
Node class   hii, What is a node class?   hello, A node class is a class that has added new services or functionality beyond the services inherited from its base class
java and xml problem. plz see this 1 first - XML
java and xml problem. plz see this 1 first  hi, i need to write a java program that generates an xml file as follows: xxx... in your XML file: "); //String str = bf.readLine(); int no = Integer.parseInt("1
Java get Node Value
Java get Node Value       In this section, you will learn how to obtain the node value. Before any further processing, you need a XML file. For this we have create
append a value to an array
append a value to an array  What?s a way to append a value to an array
tree menu delete node
nodes now i want to delete parent node and also want to delete sub nodes when parent node is deleted........i am using servlet and giving nodeid to servlet using url ....i have a problem when i perform a delete operation on parent node
Creating a Preference Node
Creating a Preference Node       This section demonstrates you to create a Preference Node... Node and System Preference Node. Here we are going to create a user preference
Append Function in Java
Append Function in Java  What is the use of append function in java? How to use the append function to append a string to a buffer string. Thanks   You can use the StringBuffer class in Java to efficiently append
MySQL Append
MySQL Append This example illustrates how to append the value of two column. In this example we use the 'CONCAT' function to append the two values of two column.   Query   SELECT
Replacing a Node with a New One
node in a DOM document. JAXP (Java API for XML Processing) is an interface which..., fchild):-This method replaces a child node with another. Xml code... Replacing a Node with a New One     
Creates element node, attribute node, comment node, processing instruction and a CDATA section
for the node Xml code for the program generated is:- <... Creates element node, attribute node, comment node, processing...; This Example shows you how to Create an Element node ,Comment node
uitextfield append text
uitextfield append text  How can i append text or string to UITextField in my iPhone application? Thanks!   Appending a string / text to a UITextField -(Void)buttonPressed { NSString *myS = [NSString stringWithFormat
How to Append String In Java?
How to Append String In Java?  How to write program in in Java for appending to a String? How to Append String In Java? What is the best way to do this? Thanks   Hi, The best best to append to a string is to use
Append Arguments to the URL
Append Arguments to the URL  Hi, How to append the query string to the Current Url when i click on a link. AM using Spring MVC i.e. My URL : http://localhost:8080/Sample/SampleApp?id=1 In my page having multiple links (a href
Replacing a Text Node with a New CDATA Section Node
()):-replaces a child node direction with CDATASection node. Xml code... Replacing a Text Node with a New CDATA Section Node... to replace a Text node with new CDATASection Node in a DOM document. Methods which
Convert Text To XML
the root and child node into xml file. Pass  array string as value of nodes... Convert Text To XML       Here we are discussing the conversion of a text file into xml
MySQL Append
MySQL Append       This example illustrates how to append the values of some columns. We use the 'CONCAT' function to append values of some columns and make
Determining If a Preference Node Exists
emits the xml document representing the node and its descendents. Here... Determining If a Preference Node Exists   ... Preference node exists or not. A preference node can be created automatically
Read XML in java - XML
Read XML in java  Hi Deepak, I want to read a xml file which have... nikumbh i tried to solve the problem but failed.the program which i hav...; nodeLst.getLength(); s++) { Node fstNode = nodeLst.item(s); Node
program to create node.
program to create node.  Hi i'm new to flex and trying to create a node and it should also have an option of right click wit any properties. how do i do this. Also i need to create a gui having drag and dop options. Please help
java.lang.IllegalArgumentException: node to traverse cannot be null!
java.lang.IllegalArgumentException: node to traverse cannot be null!  Hi, I am new to Hibernate. I am facing the problem java.lang.IllegalArgumentException: node to traverse cannot be null! while updating a row. The code
opening node in treeview struts layout - Struts
opening node in treeview struts layout  Example of opening node in treeview struts layoutopening node in treeview struts layout Hello every one, i am new to this group , i am having the same problem , Struts Layout tree view
Retrieving a Preference Node
Retrieving a Preference Node       This section demonstrates you to retrieve a Preference node. You... the Preference node. The absolute paths begin with '/'. The method name() returns
php array append
append function is used to add the element in the last position... Example of PHP Array Append Function <?php $ar1=new ArrayObject... as $a) echo $a." "; $ar1->append("ddd"); echo "<
how to write append file in Java
how to write append file in Java  How to write append file in Java   Hi, For append in the test new file or Appending a text earlier...("appenToFile.txt", true); For More Details visit this link: How to Append file
MySQL Append Data
MySQL Append Data This example illustrates how to append data with a column value. In this example we use 'CONCAT' function to append data to the column value. Table
Insert a Processing Instruction and a Comment Node
a Processing Node and Comment Node in a DOM document. JAXP (Java API for XML... for the node. Xml code for the program generated is:- <?xml... Insert a Processing Instruction and a Comment Node  
Splitting One Text Node into Three
):-This method splits the text node into two nodes at the specified offset. Xml code... Splitting One Text Node into Three       This Example describes a method to split a Text node
Locating a Node by Using Siblings
the next sibling of this node & gets its name also. Xml code for the program... Locating a Node by Using Siblings       This Example describes a method to Locate a node by using
problem in creating web application using servelet, jsp, jdbc and xml - JSP-Servlet
problem in creating web application using servelet, jsp, jdbc and xml  Using Servlet, JSP, JDBC and XML create a web application for a courrier...; Hi friend, For solving the problem visit to : http://www.roseindia.net
Convert Object To XML
into xml file as child node values. Convert Object To XMLTo create a xml file pass... Convert Object To XML       Here we are discussing the conversion of an object into xml
 

 

 

DMCA.com