• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Urgent - DOM006 Hierarchy request error

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1011
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 1011
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But I am getting the problem while adding the newly created node in between some nodes.
 
Tong Chen
Ranch Hand
Posts: 1011
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it is. I got it. Thanks a lot Tong...
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic