Java/XML/xerces
Содержание
Adding an Attribute with DOM
import org.w3c.dom.Document;
import org.apache.xerces.dom.DOMImplementation;
import org.w3c.dom.Element;
public class MainClass {
public static void main(String args[]) {
Document dom = DOMImplementation.createDocument(null, null, null);
Element root = dom.createElement("games");
Element child1 = dom.createElement("game");
root.appendChild(child1);
child1.setAttribute("A", "a");
}
}
Adding an Element with DOM
import org.w3c.dom.Document;
import org.apache.xerces.dom.DOMImplementation;
import org.w3c.dom.Element;
public class MainClass {
public static void main(String args[]) {
Document dom = DOMImplementation.createDocument(null, null, null);
Element root = dom.createElement("games");
Element child1 = dom.createElement("game");
root.appendChild(child1);
dom.appendChild(root);
}
}
A simple example to show how to use the DOM API
import java.io.*;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.apache.xerces.parsers.DOMParser;
public class MainClass {
public void processNode(Node node, String spacer) throws IOException {
if (node == null)
return;
switch (node.getNodeType()) {
case Node.ELEMENT_NODE:
String name = node.getNodeName();
System.out.print(spacer + "<" + name);
NamedNodeMap nnm = node.getAttributes();
for (int i = 0; i < nnm.getLength(); i++) {
Node current = nnm.item(i);
System.out.print(" " + current.getNodeName() + "= " + current.getNodeValue());
}
System.out.print(">");
NodeList nl = node.getChildNodes();
if (nl != null) {
for (int i = 0; i < nl.getLength(); i++) {
processNode(nl.item(i), "");
}
}
System.out.println(spacer + "</" + name + ">");
break;
case Node.TEXT_NODE:
System.out.print(node.getNodeValue());
break;
case Node.CDATA_SECTION_NODE:
System.out.print("" + node.getNodeValue() + "");
break;
case Node.ENTITY_REFERENCE_NODE:
System.out.print("&" + node.getNodeName() + ";");
break;
case Node.ENTITY_NODE:
System.out.print("<ENTITY: " + node.getNodeName() + "> </" + node.getNodeName() + "/>");
break;
case Node.DOCUMENT_NODE:
NodeList nodes = node.getChildNodes();
if (nodes != null) {
for (int i = 0; i < nodes.getLength(); i++) {
processNode(nodes.item(i), "");
}
}
break;
case Node.DOCUMENT_TYPE_NODE:
DocumentType docType = (DocumentType) node;
System.out.print("<!DOCTYPE " + docType.getName());
if (docType.getPublicId() != null) {
System.out.print(" PUBLIC " + docType.getPublicId() + " ");
} else {
System.out.print(" SYSTEM ");
}
System.out.println(" " + docType.getSystemId() + ">");
break;
default:
break;
}
}
public static void main(String[] args) {
String uri = "test.xml";
try {
bookDescDOM bd = new bookDescDOM();
System.out.println("Parsing XML File: " + uri + "\n\n");
DOMParser parser = new DOMParser();
parser.setFeature("http://xml.org/sax/features/validation", true);
parser.setFeature("http://xml.org/sax/features/namespaces", false);
parser.parse(uri);
Document doc = parser.getDocument();
bd.processNode(doc, "");
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error: " + e.getMessage());
}
}
}
Outputting a DOM with XMLSerializer
import org.w3c.dom.Document;
import org.apache.xerces.dom.DOMImplementation;
import org.w3c.dom.Element;
import org.apache.xml.serialize.XMLSerializer;
import java.io.IOException;
public class MainClass {
public static void main(String args[]) throws IOException {
Document dom = DOMImplementation.createDocument(null, null, null);
Element root = dom.createElement("A");
Element child1 = dom.createElement("B");
child1.appendChild(dom.createTextNode("C"));
child1.setAttribute("A", "a");
root.appendChild(child1);
dom.appendChild(root);
XMLSerializer serial = new XMLSerializer(System.out, null);
serial.serialize(dom.getDocumentElement());
}
}
Parsing XML with SAX
import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.InputSource;
import java.io.FileInputStream;
public class MainClass {
public static void main(String args[]) throws Exception {
SAXParser saxParser = new SAXParser();
saxParser.setContentHandler(new EventHandler());
saxParser.parse(new InputSource(new FileInputStream("games.xml")));
}
}
Walking a Document with DOM
import org.apache.xerces.parsers.DOMParser;
import org.xml.sax.SAXException;
import java.io.IOException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
public class MainClass {
public static void main(String args[]) throws IOException, SAXException {
DOMParser parser = new DOMParser();
parser.parse("games.xml");
Document dom = parser.getDocument();
walkNode(dom.getDocumentElement());
}
private static void walkNode(Node theNode) {
NodeList children = theNode.getChildNodes();
printNode(theNode);
for (int i = 0; i < children.getLength(); i++) {
Node aNode = children.item(i);
if (aNode.hasChildNodes())
walkNode(aNode);
else
printNode(aNode);
}
}
private static void printNode(Node aNode) {
System.out.println(aNode.getNodeName() + "," + aNode.getNodeValue());
}
}