Manipulating XML files using DOM via JAXP
...
TODO
What is JAXP
blabla
Creating XML files and performing XSLT transformations
The following example creates a XML configuration file and outputs it either untransformed
or transformed by a XSL stylesheet, depending on the command-line parameter (the code can be downloaded
here):
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;
import org.w3c.dom.*;
public class DOMOutput {
private int iLevel = 0;
public static void main(String args[]) throws Exception {
// here you can set your favorite parser
//System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
"org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
if (args.length==1){
new DOMOutput().run(args[0], null);
} else {
new DOMOutput().run(args[0], args[1]);
}
}
private void run(String pstrURI, String pstrXSL) throws Exception {
DocumentBuilderFactory oFactory = DocumentBuilderFactory.newInstance();
oFactory.setValidating(false);
DocumentBuilder oParser = oFactory.newDocumentBuilder();
Document oDoc = oParser.getDOMImplementation().createDocument(
"http://www.2k-software.de/javaxml/outputtest",
"config", null);
Element oRoot = oDoc.getDocumentElement();
oRoot.appendChild(oDoc.createComment(
"This is an autogenerated configuration file"));
Element oName = oDoc.createElement("name");
oName.appendChild(oDoc.createTextNode("myConfig"));
oRoot.appendChild(oName);
Element oPath1 = oDoc.createElement("path");
oPath1.setAttribute("id", "src");
oPath1.appendChild(oDoc.createTextNode("/usr/ingo/src"));
oRoot.appendChild(oPath1);
Element oPath2 = oDoc.createElement("path");
oPath2.setAttribute("id", "tmp");
oPath2.appendChild(oDoc.createTextNode("/tmp/ingo"));
oRoot.appendChild(oPath2);
Element oPath3 = oDoc.createElement("path");
oPath3.setAttribute("id", "webroot");
oPath3.appendChild(oDoc.createTextNode("/usr/ingo/webapp"));
Element oFilter3a = oDoc.createElement("filter");
oFilter3a.appendChild(oDoc.createTextNode("*.jsp"));
oPath3.appendChild(oFilter3a);
Element oFilter3b = oDoc.createElement("filter");
oFilter3b.appendChild(oDoc.createTextNode("*.html"));
oPath3.appendChild(oFilter3b);
oRoot.appendChild(oPath3);
TransformerFactory oTFactory = TransformerFactory.newInstance();
Transformer oTransformer = null;
if (pstrXSL==null){
oTransformer = oTFactory.newTransformer();
} else {
oTransformer = oTFactory.newTransformer(new StreamSource(pstrXSL));
}
//oTransformer.setOutputProperty(OutputKeys.METHOD, "xml");
oTransformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
oTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
StreamResult oResult = new StreamResult(pstrURI);
DOMSource oSource = new DOMSource(oDoc);
oTransformer.transform(oSource, oResult);
}
}
|
Reading XML files
Reading a XML file is as easy as creating one. Based on a DOMBuilder, a DOM
is constructed. This document offer Java methods for finding and working
with the document's nodes
(the code can be downloaded here):
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class DOMInput {
private int iLevel = 0;
public static void main(String args[]) throws Exception {
new DOMInput().run(args[0]);
}
private void run(String pstrURI) throws Exception {
DocumentBuilderFactory oFactory = DocumentBuilderFactory.newInstance();
oFactory.setValidating(false);
oFactory.setIgnoringElementContentWhitespace(true);
DocumentBuilder oParser = oFactory.newDocumentBuilder();
Document oDoc = oParser.parse(pstrURI);
Element oRoot = oDoc.getDocumentElement();
getTree(oRoot);
}
private void getTree(Node poNode) throws Exception {
switch(poNode.getNodeType()){
case Node.ELEMENT_NODE:
doElement(poNode);
break;
case Node.TEXT_NODE:
case Node.CDATA_SECTION_NODE:
doText(poNode);
break;
case Node.ATTRIBUTE_NODE:
doAttribute(poNode);
break;
case Node.ENTITY_REFERENCE_NODE:
doEntity(poNode);
break;
case Node.COMMENT_NODE:
doComment(poNode);
break;
case Node.PROCESSING_INSTRUCTION_NODE:
doPI(poNode);
break;
}
}
private void doElement(Node poNode) throws Exception {
System.out.println(getLeadingSpace(iLevel)+
"Element "+poNode.getNodeName());
NamedNodeMap oAttribs = poNode.getAttributes();
for (int i = 0; i<oAttribs.getLength(); i++){
iLevel++;
getTree(oAttribs.item(i));
iLevel--;
}
NodeList oNodes = poNode.getChildNodes();
for (int j=0; j<oNodes.getLength(); j++){
iLevel++;
getTree(oNodes.item(j));
iLevel--;
}
}
private void doText(Node poNode) throws Exception {
System.out.println(getLeadingSpace(iLevel)+"Text: "+
poNode.getNodeValue());
}
private void doEntity(Node poNode) throws Exception {
System.out.println(getLeadingSpace(iLevel)+"Entity: &"+
poNode.getNodeName()+";");
}
private void doAttribute(Node poNode) throws Exception {
System.out.println(getLeadingSpace(iLevel)+"Attribute: "+
poNode.getNodeName()+" = "+poNode.getNodeValue());
}
private void doComment(Node poNode) throws Exception {
System.out.println(getLeadingSpace(iLevel)+"Comment: "+
poNode.getNodeValue());
}
private void doPI(Node poNode) throws Exception {
System.out.println(getLeadingSpace(iLevel)+"PI: "+
poNode.getNodeName()+" "+poNode.getNodeValue());
}
private String getLeadingSpace(int piLevel){
StringBuffer oBuffer = new StringBuffer(20);
for (int i=0; i<piLevel; i++){
oBuffer.append(" ");
}
return oBuffer.toString();
}
}
|
Some methods
... TODO ...
Compiler prerequisites
To use JAXP/DOM with Java 1.4, neither external libraries nor installation steps
are to be respected.
back to toolbox page
|