Skip to content

Instantly share code, notes, and snippets.

@bzdgn
Created December 9, 2015 21:22
Show Gist options
  • Save bzdgn/1787e7ce85eafd65b66a to your computer and use it in GitHub Desktop.
Save bzdgn/1787e7ce85eafd65b66a to your computer and use it in GitHub Desktop.
JAVA DocumentBuilderFactory Example Code
package com.levo.abstractfactory.example;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class AdvancedDocumentBuilderFactoryDemo {
private static final int INDENT_SIZE = 3;
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
// String xml = "<document><body><guitar>Ibanez</guitar></body></document>";
String xml = "<document><body><guitar>Ibanez</guitar><guitar>Gibson</guitar></body></document>";
ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
// Demonstrate how to traverse ByteArrayInputStream
int length = bais.available();
for(int i = 0; i < length; i++) {
System.out.printf("%c", bais.read());
}
bais.reset();
DocumentBuilderFactory abstractFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder factory = abstractFactory.newDocumentBuilder();
Document doc = factory.parse(bais);
doc.getDocumentElement().normalize();
System.out.println("Root element : " + doc.getDocumentElement().getNodeName());
System.out.println(abstractFactory.getClass());
System.out.println(factory.getClass());
// printDocumentTree(doc);
printDocumentTreeWithIndent(doc);
}
private static void printDocumentTree(Document doc) {
Element root = doc.getDocumentElement();
System.out.println(root.getNodeName() + " : " + root.getNodeValue());
NodeList children = root.getChildNodes();
if(children != null) {
printDocumentNodeList(root.getChildNodes());
}
}
private static void printDocumentNodeList(NodeList children) {
for(int i = 0; i < children.getLength(); i++) {
System.out.println(children.item(i).getNodeName() + " : " + children.item(i).getNodeValue());
if(children.item(i).hasChildNodes()) {
printDocumentNodeList(children.item(i).getChildNodes());
}
}
}
private static void printDocumentTreeWithIndent(Document doc) {
Element root = doc.getDocumentElement();
System.out.println("<" + root.getNodeName() + " : " + root.getNodeValue() + ">");
NodeList children = root.getChildNodes();
if(children != null) {
printDocumentNodeListWithIndent(root.getChildNodes(), INDENT_SIZE);
}
}
private static void printDocumentNodeListWithIndent(NodeList children, int height) {
for(int i = 0; i < children.getLength(); i++) {
printIndent(height);
System.out.println("<" + children.item(i).getNodeName() + " : " + children.item(i).getNodeValue() + ">");
if(children.item(i).hasChildNodes()) {
height += INDENT_SIZE;
printDocumentNodeListWithIndent(children.item(i).getChildNodes(), height);
}
height -= INDENT_SIZE;
}
}
private static void printIndent(int indent) {
for(int i = 0; i < indent; i++) {
System.out.print(".");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment