Java Reference
In-Depth Information
The first approach runs into problems with whitespace. This document has carriage returns
and tabs in it, and because no DTD or schema is provided, the parser doesn't know which
whitespace elements are significant. Traversing the DOM is complicated by the fact that
methods like getFirstChild will return whitespace nodes as well as elements. It can
be done, but you'll need to check the node type of each element to make sure you are work-
ing with an element rather than a text node.
The second approach only works if the elements have an attribute of type ID , and that's
not the case here.
You're left with the getElementsByTagName method, which results in the following
code:
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 ProcessBooks {
public static void main(String[] args) {
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
Document doc = null;
try {
DocumentBuilder builder = factory.newDocumentBuilder();
doc = builder.parse("books.xml");
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
NodeList titles = doc.getElementsByTagName("title");
Element titleNode = (Element) titles.item(1);
String title = titleNode.getFirstChild().getNodeValue();
System.out.println("The second title is " + title);
}
}
Search WWH ::




Custom Search