aspose file tools*
The moose likes Product and Other Certifications and the fly likes Urgent - DOM006 Hierarchy request error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Product and Other Certifications
Bookmark "Urgent - DOM006 Hierarchy request error" Watch "Urgent - DOM006 Hierarchy request error" New topic
Author

Urgent - DOM006 Hierarchy request error

Md Fizal
Ranch Hand

Joined: Dec 23, 2002
Posts: 61
I have an input XML file, which I converted into a DOM object. I need to add nodes dynamically to the DOM object before I process with it. When I tried appending child nodes to it, I am getting "org.w3c.dom.DOMException: DOM006 Hierarchy request error" exception.
Could you anyone please help me solve this?
My input XML goes something like this :
<xml......>
<Control/>
<Request>
<Date/>
<Text/>
<Pictures/>
</Request>
I wanna add my child nodes to the 'Request' node in the input.
Tong Chen
Ranch Hand

Joined: Apr 26, 2002
Posts: 1011
Hi, Fiz,
First of all, the XML doc you tried to process is not well-formed. You need a root element for this doc.
I changed you file and added a root and saved it a file called "testXML.xml".
Here is the Java code to add a new element node under "Request" node.
package com.tongchen.domtest;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import java.io.File;
import org.w3c.dom.*;
import org.apache.xml.serialize.XMLSerializer;
import org.apache.xml.serialize.OutputFormat;
public class TestProcessor {
//This is a file path
private static final String filePath = "C:\\Program Files\\IBM\\Application Developer\\workspace_Tong\\XML\\XMLFiles\\";
public static void main (String args[]) {
File docFile = new File(filePath + "testXML.xml");
Document doc = null;

try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = factory.newDocumentBuilder();
doc = parser.parse(docFile);
}
catch (javax.xml.parsers.ParserConfigurationException pce) {
System.err.println("The parser was not configured properly.");
System.exit(1);
}
catch (java.io.IOException ioe) {
System.err.println("Can not read the input file.");
System.exit(1);
}
catch (org.xml.sax.SAXException se) {
System.err.println("Problem parsing the file.");
System.exit(1);
}
catch (java.lang.IllegalArgumentException iae) {
System.err.println("Please specify an XML source.");
System.exit(1);
}

//step 1: get the root element;
Element root = doc.getDocumentElement();
//step 2: step through the children
for (Node child = root.getFirstChild();
child != null;
child = child.getNextSibling()) {

if (child.getNodeName().equalsIgnoreCase("Request")) {
Element newElement = doc.createElement("newElement");
newElement.appendChild(doc.createTextNode("newContent"));
child.appendChild(newElement);

}

}

//step3: output the new XML file
try {
OutputFormat formatter = new OutputFormat();
formatter.setPreserveSpace(true);
XMLSerializer serializer = new XMLSerializer(System.out, formatter);
serializer.serialize(doc);
}
catch (java.io.IOException ioe) {
System.err.println("Error happened while serializing the new XML file.");
System.exit(1);
}
}

}
Hope this helps you.


Tong Chen (Seattle USA)<br />SCJP,SCWCD,SCDJWS,IBM XML,MCP.NET,MCAD.NET,MCSD.NET
Tong Chen
Ranch Hand

Joined: Apr 26, 2002
Posts: 1011
Besides, here is the output XML file you will see:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<Control/>
<Request>
<Date/>
<Text/>
<Pictures/>
<newElement>newContent</newElement></Request>
</root>
You can see the new element <newElement> is added under the "Request" node.
Md Fizal
Ranch Hand

Joined: Dec 23, 2002
Posts: 61
But I am getting the problem while adding the newly created node in between some nodes.
Tong Chen
Ranch Hand

Joined: Apr 26, 2002
Posts: 1011
OK, Fiz, Here is the code snippet:
package com.tongchen.domtest;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import java.io.File;
import org.w3c.dom.*;
import org.apache.xml.serialize.XMLSerializer;
import org.apache.xml.serialize.OutputFormat;
public class TestProcessor {
//This is a file path
private static final String filePath = "C:\\Program Files\\IBM\\Application Developer\\workspace_Tong\\XML\\XMLFiles\\";
public static void main (String args[]) {
File docFile = new File(filePath + "testXML.xml");
Document doc = null;

try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = factory.newDocumentBuilder();
doc = parser.parse(docFile);
}
catch (javax.xml.parsers.ParserConfigurationException pce) {
System.err.println("The parser was not configured properly.");
System.exit(1);
}
catch (java.io.IOException ioe) {
System.err.println("Can not read the input file.");
System.exit(1);
}
catch (org.xml.sax.SAXException se) {
System.err.println("Problem parsing the file.");
System.exit(1);
}
catch (java.lang.IllegalArgumentException iae) {
System.err.println("Please specify an XML source.");
System.exit(1);
}

//step 1: get the root element;
Element root = doc.getDocumentElement();
//step 2: step through the children
for (Node child = root.getFirstChild();
child != null;
child = child.getNextSibling()) {

if (child.getNodeName().equalsIgnoreCase("Request")) {
for (Node innerChild = child.getFirstChild();
innerChild != null;
innerChild = innerChild.getNextSibling()) {

if (innerChild.getNodeName().equalsIgnoreCase("Text")) {

Element newElement = doc.createElement("newElement");
newElement.appendChild(doc.createTextNode("newContent"));
child.insertBefore(newElement, innerChild);

}

}

}

}

//step3: output the new XML file
try {
OutputFormat formatter = new OutputFormat();
formatter.setPreserveSpace(true);
XMLSerializer serializer = new XMLSerializer(System.out, formatter);
serializer.serialize(doc);
}
catch (java.io.IOException ioe) {
System.err.println("Error happened while serializing the new XML file.");
System.exit(1);
}
}

}
---------------------------------------------
input XML file (in testXML.xml):
<?xml version="1.0" encoding="UTF-8"?>
<root>
<Control/>
<Request>
<Date/>
<Text/>
<Pictures/>
</Request>
</root>
-----------------------------------------
the output XML file will be:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<Control/>
<Request>
<Date/>
<newElement>newContent</newElement>
<Text/>
<Pictures/>
</Request>
</root>
You will see the <newElement> is inserted between <Date> and <Text>
Is this what you want?
Have a fun!
Md Fizal
Ranch Hand

Joined: Dec 23, 2002
Posts: 61
Yes, it is. I got it. Thanks a lot Tong...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Urgent - DOM006 Hierarchy request error