Parse XML file with DOM Parser
In this chapter you will learn:
Parse an XML File with DOM
The following code parses an XML file with DOM parser.
From the code we can see that we can create a File
object
and pass into the DocumentBuilder
directly.
import java.io.File;
/*from j av a2 s . co m*/
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
public class MainClass {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File("employee.xml"));
}
}
Here is the employee.xml
file.
<data>/*from ja v a 2 s . c om*/
<employee>
<name>Tom</name>
<title>Manager</title>
</employee>
<employee>
<name>Jerry</name>
<title>Programmer</title>
</employee>
</data>
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » XML