Article Directory
1 XML parsing Introduction (written Java code)
- xml document type is marked
- Dom parse tag type using the JS file:
| - html The hierarchical structure of a tree structure in the allocated memory, the html tags, attributes, and text are encapsulated into objects.
| --Document objects, element objects, properties, objects, text objects, Node node object - xml parsing mode (technology): dom and sax
2 JAXP API View
I want to parse xml, you first need parser. Different companies and organizations dom and sax parser for ways to provide by way of api. sun has provided --jaxp for dom and sax parser; dom4j organizations provided for dom and sax parser - dom4j (most use the actual development); jdom organization provides --jdom for dom and sax parser.
The JAXP API
| --jaxp is javase part
| --jaxp parser javax.xml.parsers jdk package inside
| ---- four categories: dom and sax, respectively, for the use of analytical categories
| ---- dom:
| ------ DocumentBuilder
: parser class, which is an abstract class, not new. Such through DocumentBuilderFactory.newDocumentBuilder()
acquisition method. In this class public Document parse(File f) throws SAXException, IOException
and public Document parse(String uri) throws SAXException,IOException
can parse xml, it returns a Document object. Document object is returned but an interface, in the package org.w3c.dom
, the parent node is Node, if desired in the interface method is not found inside the Document, which look to the Node.
Interface public interface Document extends Node
Summary
No. | Method name | Method Type | The method explained |
---|---|---|---|
01 | NodeList getElementsByTagName(String tagname) | Common method | Tag can be returned to a collection NodeList |
02 | Element createElement(String tagName) throws DOMException | Common method | Create labels, returns an Element object |
03 | Text createTextNode(String data) | Common method | Create text, create a text object returns |
04 | Node appendChild(Node newChild) throws DOMException | Common method | To add text to the label below, the node object returns the added |
05 | Node removeChild(Node oldChild) throws DOMException | Common method | To delete a node, return node object is deleted |
06 | Node getParentNode() | Common method | Get the parent node |
Interface public interface NodeList
Summary
No. | Method name | Method Type | The method explained |
---|---|---|---|
01 | int getLength() | Common method | Length set to give |
02 | Node item(int index) | Common method | Specific values obtained subscript |
Interface public interface Node
Summary
No. | Method name | Method Type | The method explained |
---|---|---|---|
01 | String getTextContent() throws DOMException | Common method | Get the contents of the label |
GetTextContent String ()
throws DOMException
| ------ DocumentBuilderFactory
: parser factory, this class is an abstract class, not new. However, you can use an abstract class of static methods newInstance()
to obtain DocumentBuilderFactory
instances
| ---- SAX:
| ------ SAXParser
: parser class
| ------ SAXParserFactory
: parser factory
3 Use the query node JAXP
<?xml version="1.0" encoding="UTF-8"?>
<person>
<p1>
<name>lks</name>
<age>23</age>
</p1>
<p1>
<name>hhy</name>
<age>20</age>
</p1>
</person>
Query the value of all the elements of XML.
(1) Creating a parser facility
(2) according to the parser to create a parser facility
(3) Returns parsed xml Document object
(4) to give the name of all the elements
(5) through the collection, to obtain a name of each element
private static void selectAll() throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory docbuilderfactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docbuilder = docbuilderfactory.newDocumentBuilder();
Document doc = docbuilder.parse("src/dtdlearn/mydtd00/person.xml");
NodeList nodelist = doc.getElementsByTagName("name");
for(int i = 0; i < nodelist.getLength(); i++) {
Node n = nodelist.item(i);
String str = n.getTextContent();
System.out.println(str);
}
}
4 Use JAXP query a node
XML query value in the first element of the name.
(1) Creating a parser facility
(2) according to the parser to create a parser facility
(3) Returns parsed xml Document object
(4) to give the name of all the elements
(5) using the method which returns a collection of elements for specific index item
(6) to give specific values, using getTextContent ()
private static void searchElement() throws Exception{
DocumentBuilderFactory documentbuilderfactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentbuilder = documentbuilderfactory.newDocumentBuilder();
Document document = documentbuilder.parse("src/dtdlearn/mydtd00/person.xml");
NodeList nodelist = document.getElementsByTagName("name");
Node node = nodelist.item(0);
System.out.println(node.getTextContent());
}
5 add nodes using JAXP
Add in a first p1 below <sex>女</sex>
.
(1) Creating a parser facility
(2) according to the parser to create a parser facility
(3) Returns parsed xml Document object
(4) to obtain a first p1, get all the p1, the use of subscripts to give item
(5) to create labels sex createElement
(6) create text createTextNode
(7) to add the following text to the sex appendChild
(8) adding sex to the first p1 below
(9) write-back xml transformer
private static void addElement() throws Exception{
DocumentBuilderFactory documentbuilderfactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentbuilder = documentbuilderfactory.newDocumentBuilder();
Document document = documentbuilder.parse("src/dtdlearn/mydtd00/person.xml");
NodeList nodelist = document.getElementsByTagName("p1");
Node node = nodelist.item(0);
Node sex = document.createElement("sex");
Text text = document.createTextNode("女");
sex.appendChild(text);
node.appendChild(sex);
TransformerFactory transformerfactory = TransformerFactory.newInstance();
Transformer transformer = transformerfactory.newTransformer();
transformer.transform(new DOMSource(document), new StreamResult("src/dtdlearn/mydtd00/person.xml"));
}
6 using the modified node JAXP
Modify the first content male sex p1 is the following
(1) to create a parser facility
(2) according to the parser to create a parser facility
(3) Returns parsed xml Document object
(4) obtained in the sex p1
(. 5) which modify sex the value setTextContent ()
(6) write-back xml transformer
public static void modifyElement() throws Exception{
DocumentBuilderFactory documentbuilderfactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentbuilder = documentbuilderfactory.newDocumentBuilder();
Document document = documentbuilder.parse("src/dtdlearn/mydtd00/person.xml");
NodeList nodelist = document.getElementsByTagName("sex");
Node node = nodelist.item(0);
node.setTextContent("男");
TransformerFactory transformerfactory = TransformerFactory.newInstance();
Transformer transformer = transformerfactory.newTransformer();
transformer.transform(new DOMSource(document), new StreamResult("src/dtdlearn/mydtd00/person.xml"));
}
7 delete nodes using JAXP
Sex node under delete p1.
(1) Creating a parser facility
(2) according to the parser to create a parser facility
(3) Returns parsed xml Document object
(4) Get sex element
(5) Get parent node sex element getParentNode
(. 6) by the parent node deletes sex node
(7) write-back xml
public static void deleteElement() throws Exception{
DocumentBuilderFactory documentbuilderfactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentbuilder = documentbuilderfactory.newDocumentBuilder();
Document document = documentbuilder.parse("src/dtdlearn/mydtd00/person.xml");
NodeList nodelist = document.getElementsByTagName("sex");
Node node = nodelist.item(0);
Node parent = node.getParentNode();
parent.removeChild(node);
TransformerFactory transformerfactory = TransformerFactory.newInstance();
Transformer transformer = transformerfactory.newTransformer();
transformer.transform(new DOMSource(document), new StreamResult("src/dtdlearn/mydtd00/person.xml"));
}
8 node traversal using JAXP
Xml all the names printed out.
public static void traverseElement() throws Exception{
DocumentBuilderFactory documentbuilderfactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentbuilder = documentbuilderfactory.newDocumentBuilder();
Document document = documentbuilder.parse("src/dtdlearn/mydtd00/person.xml");
list(document.getFirstChild());
}
public static void list(Node node) {
if(node.getNodeType()==Node.ELEMENT_NODE) {
System.out.println(node.getNodeName());
}
NodeList nodelist = node.getChildNodes();
for(int i = 0; i < nodelist.getLength(); i++) {
Node node1 = nodelist.item(i);
list(node1);
}
}