Parsing XML with DOM

File file = new File("document.xml");
											DocumentBuilderFactory f =
											DocumentBuilderFactory.newInstance();
											DocumentBuilder p = f.newDocumentBuilder();
											Document doc = p.parse(file);

The DocumentBuilderFactory, DocumentBuilder, and Document classes are the three classes that we use to kick off the parsing of an XML document using a DOM parser. We perform the parsing with the DocumentBuilder class. The DocumentBuilder class defines the API to obtain DOM Document instances from an XML document. The DocumentBuilder class can parse XML from a variety of input sources, including InputStreams, Files, URLs, and SAXInputSources. In this phrase, we parse the XML from a File input source. The parse() method of the DocumentBuilder class parses ...

Get Java™ Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.