D
dmckeon
I am new to Java and XML, so forgive any ignorance. I am working on a
program to create an XML document and I am having some problems with
nesting. I want something like the following:
<?xml version="1.0" encoding="UTF-8" ?>
<Employees>
<EMPLOYEE>
<NAME>
<FIRST>John</FIRST>
<LAST>Doe</LAST>
</NAME>
<TITLE>Mr.</TITLE>
<WAGE>
<RATE>Salaried</RATE>
<WAGE>40,000</WAGE>
</WAGE>
</EMPLOYEE>
</Employees>
But instead, I am getting:
<?xml version="1.0" encoding="UTF-8" ?>
<Employees>
<NAME>
<FIRST>Jeff</FIRST>
<LAST>Fry</LAST>
</NAME>
<TITLE>Senior</TITLE>
<WAGE>
<RATE>Salaried</RATE>
<WAGE>40,000</WAGE>
</WAGE>
<EMPLOYEE />
</Emplyees>
The difference being the EMPLOYEE tag. It appears to be a problem
calling the startParent method twice in a row. Here is my XML code:
package igxml;
import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.*;
import org.apache.xml.serialize.*;
public class IgXMLBuild extends DefaultHandler {
Document doc;
Element root;
Element parentElement;
Element childElement;
Text textData;
public void startDocument(String docName)
{
// Create the DOM object
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
System.out.println("Could not locate a JAXP DocumentBuilder
class");
}
doc = builder.newDocument();
/* create the root node */
root = doc.createElement(docName);
return;
}
public Element startParent (String parentName)
{
/* create a row node */
parentElement = doc.createElement(parentName);
return parentElement;
}
public void endParent (Element parentElement)
{
/* append the row node to the root node */
root.appendChild(parentElement);
}
public void addParent(String passParentName, String
passParentValue)
{
parentElement = startParent(passParentName);
parentElement.appendChild(doc.createTextNode(passParentValue));
endParent(parentElement);
}
public void addChild(String passElementName, String passElementValue)
{
childElement = doc.createElement(passElementName);
textData = doc.createTextNode(passElementValue);
childElement.appendChild(textData);
parentElement.appendChild(childElement);
}
public String finishDocument()
{
/* append the root node to the empty document */
doc.appendChild(root);
try {
// Serialize the document
StringWriter out;
out = new StringWriter();
XMLSerializer serializer = new XMLSerializer(out, new
OutputFormat(doc));
serializer.serialize(doc);
String xmlString = out.toString();
return xmlString;
} catch (IOException e) {
System.err.println(e);
return e.toString();
}
}
}
And here is the test application I am using to call these methods:
package igxml;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.w3c.dom.*;
public class TestApp {
static DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS
");
public static void logit(String m){
System.out.println( df.format(new Date()) + m);
}
public static void main(String[] args) {
/* logit("TestApp started...");
String tranReqDoc, docList;
ImgDocList dl = new ImgDocList();
tranReqDoc = "<?xml version='1.0'
encoding='UTF-8'?><DATA><IMG-APP-ID>PS</IMG-APP-ID><IMG-FOLDER-ID>1111111111</IMG-FOLDER-ID><IMG-USER-ID>ISJAF</IMG-USER-ID></DATA>";
docList = dl.getDocList(tranReqDoc);
System.out.println(docList);
logit("TestApp finished...");*/
Element empElement;
Element nameElement;
Element wageElement;
IgXMLBuild xmlDoc = new IgXMLBuild();
xmlDoc.startDocument("Employees");
empElement = xmlDoc.startParent("EMPLOYEE");
nameElement = xmlDoc.startParent("NAME");
xmlDoc.addChild("FIRST", "John");
xmlDoc.addChild("LAST", "Doe");
xmlDoc.endParent(nameElement);
xmlDoc.addParent("TITLE", "Mr.");
wageElement = xmlDoc.startParent("WAGE");
xmlDoc.addChild("RATE", "Salaried");
xmlDoc.addChild("AMMOUNT", "40,000");
xmlDoc.endParent(wageElement);
xmlDoc.endParent(empElement);
empElement = xmlDoc.startParent("EMPLOYEE");
nameElement = xmlDoc.startParent("NAME");
xmlDoc.addChild("FIRST", "Jane");
xmlDoc.addChild("LAST", "Doe");
xmlDoc.endParent(nameElement);
xmlDoc.addParent("TITLE", "Mrs.");
wageElement = xmlDoc.startParent("WAGE");
xmlDoc.addChild("RATE", "Hourly");
xmlDoc.addChild("AMMOUNT", "42.00");
xmlDoc.endParent(wageElement);
xmlDoc.endParent(empElement);
empElement = xmlDoc.startParent("EMPLOYEE");
nameElement = xmlDoc.startParent("NAME");
xmlDoc.addChild("FIRST", "John");
xmlDoc.addChild("LAST", "Smith");
xmlDoc.endParent(nameElement);
xmlDoc.addParent("TITLE", "Sir");
wageElement = xmlDoc.startParent("WAGE");
xmlDoc.addChild("RATE", "Salaried");
xmlDoc.addChild("AMMOUNT", "48,000");
xmlDoc.endParent(wageElement);
xmlDoc.endParent(empElement);
String xmlString = xmlDoc.finishDocument();
System.out.println(xmlString);
}
}
Any idea why I can't nest that extra level???
program to create an XML document and I am having some problems with
nesting. I want something like the following:
<?xml version="1.0" encoding="UTF-8" ?>
<Employees>
<EMPLOYEE>
<NAME>
<FIRST>John</FIRST>
<LAST>Doe</LAST>
</NAME>
<TITLE>Mr.</TITLE>
<WAGE>
<RATE>Salaried</RATE>
<WAGE>40,000</WAGE>
</WAGE>
</EMPLOYEE>
</Employees>
But instead, I am getting:
<?xml version="1.0" encoding="UTF-8" ?>
<Employees>
<NAME>
<FIRST>Jeff</FIRST>
<LAST>Fry</LAST>
</NAME>
<TITLE>Senior</TITLE>
<WAGE>
<RATE>Salaried</RATE>
<WAGE>40,000</WAGE>
</WAGE>
<EMPLOYEE />
</Emplyees>
The difference being the EMPLOYEE tag. It appears to be a problem
calling the startParent method twice in a row. Here is my XML code:
package igxml;
import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.*;
import org.apache.xml.serialize.*;
public class IgXMLBuild extends DefaultHandler {
Document doc;
Element root;
Element parentElement;
Element childElement;
Text textData;
public void startDocument(String docName)
{
// Create the DOM object
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
System.out.println("Could not locate a JAXP DocumentBuilder
class");
}
doc = builder.newDocument();
/* create the root node */
root = doc.createElement(docName);
return;
}
public Element startParent (String parentName)
{
/* create a row node */
parentElement = doc.createElement(parentName);
return parentElement;
}
public void endParent (Element parentElement)
{
/* append the row node to the root node */
root.appendChild(parentElement);
}
public void addParent(String passParentName, String
passParentValue)
{
parentElement = startParent(passParentName);
parentElement.appendChild(doc.createTextNode(passParentValue));
endParent(parentElement);
}
public void addChild(String passElementName, String passElementValue)
{
childElement = doc.createElement(passElementName);
textData = doc.createTextNode(passElementValue);
childElement.appendChild(textData);
parentElement.appendChild(childElement);
}
public String finishDocument()
{
/* append the root node to the empty document */
doc.appendChild(root);
try {
// Serialize the document
StringWriter out;
out = new StringWriter();
XMLSerializer serializer = new XMLSerializer(out, new
OutputFormat(doc));
serializer.serialize(doc);
String xmlString = out.toString();
return xmlString;
} catch (IOException e) {
System.err.println(e);
return e.toString();
}
}
}
And here is the test application I am using to call these methods:
package igxml;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.w3c.dom.*;
public class TestApp {
static DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS
");
public static void logit(String m){
System.out.println( df.format(new Date()) + m);
}
public static void main(String[] args) {
/* logit("TestApp started...");
String tranReqDoc, docList;
ImgDocList dl = new ImgDocList();
tranReqDoc = "<?xml version='1.0'
encoding='UTF-8'?><DATA><IMG-APP-ID>PS</IMG-APP-ID><IMG-FOLDER-ID>1111111111</IMG-FOLDER-ID><IMG-USER-ID>ISJAF</IMG-USER-ID></DATA>";
docList = dl.getDocList(tranReqDoc);
System.out.println(docList);
logit("TestApp finished...");*/
Element empElement;
Element nameElement;
Element wageElement;
IgXMLBuild xmlDoc = new IgXMLBuild();
xmlDoc.startDocument("Employees");
empElement = xmlDoc.startParent("EMPLOYEE");
nameElement = xmlDoc.startParent("NAME");
xmlDoc.addChild("FIRST", "John");
xmlDoc.addChild("LAST", "Doe");
xmlDoc.endParent(nameElement);
xmlDoc.addParent("TITLE", "Mr.");
wageElement = xmlDoc.startParent("WAGE");
xmlDoc.addChild("RATE", "Salaried");
xmlDoc.addChild("AMMOUNT", "40,000");
xmlDoc.endParent(wageElement);
xmlDoc.endParent(empElement);
empElement = xmlDoc.startParent("EMPLOYEE");
nameElement = xmlDoc.startParent("NAME");
xmlDoc.addChild("FIRST", "Jane");
xmlDoc.addChild("LAST", "Doe");
xmlDoc.endParent(nameElement);
xmlDoc.addParent("TITLE", "Mrs.");
wageElement = xmlDoc.startParent("WAGE");
xmlDoc.addChild("RATE", "Hourly");
xmlDoc.addChild("AMMOUNT", "42.00");
xmlDoc.endParent(wageElement);
xmlDoc.endParent(empElement);
empElement = xmlDoc.startParent("EMPLOYEE");
nameElement = xmlDoc.startParent("NAME");
xmlDoc.addChild("FIRST", "John");
xmlDoc.addChild("LAST", "Smith");
xmlDoc.endParent(nameElement);
xmlDoc.addParent("TITLE", "Sir");
wageElement = xmlDoc.startParent("WAGE");
xmlDoc.addChild("RATE", "Salaried");
xmlDoc.addChild("AMMOUNT", "48,000");
xmlDoc.endParent(wageElement);
xmlDoc.endParent(empElement);
String xmlString = xmlDoc.finishDocument();
System.out.println(xmlString);
}
}
Any idea why I can't nest that extra level???