Java 7 for Absolute Beginners

(nextflipdebug5) #1

CHAPTER 9 ■ WRITING AND READING XML


public class ReadWithDOM {

public static void main(String[] args) {
String fileName = "C:" + File.separator + "test"
+ File.separator + "poemsource.xml";
writeFileContentsToConsole(fileName);
}

// Write the contents of the file to the console private static void
writeFileContentsToConsole(String fileName) {
// Create a DOM Document object Document doc = createDocument(fileName);
// Get the root element Element root = doc.getDocumentElement();
// Create a StringBuilder object that describes the root element StringBuilder sb = new
StringBuilder(); sb.append("The root element is named: \"" + root.getNodeName() + "\"");
sb.append(" and has the following attributes: ");
NamedNodeMap attributes = root.getAttributes();
for (int i = 0; i < attributes.getLength(); i ++) {
Node thisAttribute = attributes.item(i);
sb.append(thisAttribute.getNodeName());
sb.append (" (\"" + thisAttribute.getNodeValue() + "\")");
if (i < attributes.getLength() - 1) {
sb.append(", ");
}
}
// Write the description of the root element to the console System.out.println(sb);

// Work through the children of the root
// First, get a list of the child nodes NodeList nodes =
doc.getElementsByTagName("line");
for (int i = 0; i < nodes.getLength(); i++) {
// Process each element in turn Element element = (Element) nodes.item(i);
System.out.println("Found an element named \"" +
// By writing its name and content to the console (System.out)
element.getTagName() + "\"" +
" with the following content: \"" +
element.getTextContent() + "\"");
}
}

// Create a DOM Document object from a file private static Document createDocument(String
fileName) {
Document doc = null;
try {
// Get the file File xmlFile = new File(fileName);
// Create document builder factory DocumentBuilderFactory dbfac =
DocumentBuilderFactory.newInstance();
// Create a document builder object DocumentBuilder docBuilder =
dbfac.newDocumentBuilder();
// Load the document by parsing the file with the document builder doc =
docBuilder.parse(xmlFile);
// Indicate that this document is self-contained doc.setXmlStandalone(true);
}
Free download pdf