程序代写代做代考 Java database PowerPoint Presentation

PowerPoint Presentation

WaysInJavaToParseXML

Prof: Dr. Shu-Ching Chen
TA: Sheng Guan

Parse XML with Java DOM API
How to do this ?
The Java DOM API for XML parsing is intended for working with XML as an object graph in memory – a “Document Object Model (DOM)”.
The parser traverses the XML file and creates the corresponding DOM objects.
DOM objects are linked together in a tree structure.

*

Object model ( DOM Tree)

Steps:

XML file and
corresponding DOM structure

Fun Software
Jakob Jenkov
0123456789

*

DOM – 3 pieces of XML
1. Elements (sometimes called tags)
2. Attributes
3. The data (also called values) that the elements and attributes describe

*

Step 1:
Creating a Java DOM XML parser
Creating a Java DOM XML parser is done using the javax.xml.parsers.DocumentBuilderFactory class. Here is an example:

DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = builderFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}

*

Step 2:
Parsing an XML file into a DOM tree
try {
Document document = builder.parse( new FileInputStream(“/path/to/your/file.xml”));
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

*

Step 3:
Java DOM: Get the Document Object
You are now ready to traverse the Document instance you have received from the DocumentBuilder;

The DOM Document object represents an XML document. When you parse an XML file using a Java DOM parser, you get back a Document object.

*

The DOM Document Element

The two most commonly used features of DOM are:

1.Accessing Child Elements of an Element
2.Accessing Attributes of an Element
A DOM object contains a lot of different nodes connected in a tree-like structure. At the top is the Document object.
The Document object has a single root element, which is returned by calling getDocumentElement():
Element rootElement = document.getDocumentElement();

*

DOM Elements, Child Elements, and the Node Interface
The root element has children. You get the children of an element like this:
NodeList nodes = element.getChildNodes();
for(int i=0; i