Dom :( DocumentObjectModel, that is, the Document Object Model) is a method recommended by W3C to process XML. XML programming-DOM
XML parsing technology
Xml parsing is commonly used in two types: dom parsing and sax parsing.
Dom :( Document Object Model, that is, Document Object Model) is a method recommended by W3C organizations to process XML.
Sax :( Simple API for XML) is not an official standard, but it is a de facto standard of the XML community. almost all XML parser supports it.
Jaxp introduction
Jaxp (Java API for XML Processing) is a Java Development Kit for XML programming. it consists of javax. xml, org. w3c. dom, org. xml. sax package and its sub-packages.
In the javax. xml. parsers package, several factory classes are defined. programmers call these factory classes to obtain DOM or SAX parser objects for parsing xml documents.
DOM overview
DOM (Document Object Model) is a standard programming interface recommended by W3C for processing extensible markup language. The xml dom defines the objects and attributes of all XML elements and the methods (interfaces) for accessing them ).
Schematic diagram
When parsing an XML document, the DOM parser parses all the elements in the document into a Node object (Node) based on the hierarchical relationship they appear ).
In dom, the relationship between nodes is as follows:
1. a node located above a node is the parent node of the node (parent)
2. a node under a node is the child node of the node)
3. at the same level, a node with the same parent node is a sibling node (sibling)
4. the node set at the next layer of a node is the node descendant (descendant)
5. the parent and grandfather nodes and all those on the node are the ancestor of the node)
Node object
The Node object provides a series of constants to represent the Node type. after a developer obtains a Node type, the Node can be converted to a corresponding Node object (a subclass object of the Node ), to call its unique method. (View API documentation)
The Node object provides corresponding methods to obtain its parent Node or child Node. With these methods, programmers can read the content of the entire XML document, or add, modify, or delete the content of the XML document.
PS: its subinterface Element has more functions.
Get the DOM parser in Jaxp
1. call the DocumentBuilderFactory. newInstance () method to create the DOM parser factory.
2. call the newDocumentBuilder () method of the DocumentBuilderFactory object to obtain the DOM parser object, which is the object of DocumentBuilder.
3. call the parse () method of the DocumentBuilder object to parse the XML Document and obtain the Document object representing the entire Document.
4. operate XML documents through Document objects and related classes and methods.
Update XML documents
The Transformer class in the javax. xml. transform package is used to convert the Document object representing the XML file into a certain format and then output it. for example, the xml file is converted into an html Document after applying the style sheet. This object can also be used to re-write the Document object to an XML file.
The Transformer class completes the conversion operation through the transform method, which receives one source and one destination. We can use:
Javax. xml. transform. dom. DOMSource class to associate the document object to be converted,
Use the javax. xml. transform. stream. StreamResult object to represent the data destination.
The Transformer object is obtained through TransformerFactory.
Case:
XML5.xml
<班级 班次="1班" 编号="C1">
<学生 地址="湖南" 学号="n1" 性别="男" 授课方式="面授" 朋友="n2" 班级编号="C1">
<名字>
Zhang San
<年龄>
20
<介绍>
Good
<学生 学号="n2" 性别="女" 授课方式="面授" 朋友="n1 n3" 班级编号="C1">
<名字>
Li Si
<年龄>
18
<介绍>
Good
<学生 学号="n3" 性别="男" 授课方式="面授" 朋友="n2" 班级编号="C1">
<名字>
Wang Wu
<年龄>
22
<介绍>
Very good
<学生 性别="男">
<名字>
James
<年龄>
30
<介绍>
Good
Package com. pc; import java. awt. list; import java. util. arrayList; import javax. xml. parsers. documentBuilder; import javax. xml. parsers. documentBuilderFactory; import javax. xml. transform. transformer; import javax. xml. transform. transformerConfigurationException; import javax. xml. transform. transformerException; import javax. xml. transform. transformerFactory; import javax. xml. transform. transformerFactoryConfigurationError; import javax. xml. transform. dom. DOMSource; import javax. xml. transform. stream. streamResult; import org. w3c. dom. document; import org. w3c. dom. element; import org. w3c. dom. node; import org. w3c. dom. nodeList; /***** @ author Switch * @ function Java parse XML **/public class XML5 {// use dom technology to operate the xml file public static void main (String [] args) throws Exception {// 1. create a DocumentBuilderFactory object DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory. newInstance (); // 2. get a DocumentBuilder object DocumentBuilder documentBuilder = DocumentBuilderFactory through documentBuilderFactory. newDocumentBuilder (); // 3. specify the xml file to be parsed, Document document = documentBuilder. parse ("src/com/pc/XML5.xml"); // 4. operations on XML Documents // System. out. println (document); // list (document); // read (document); // add (document); // delete (document, "James "); update (document, "James", "30");} // update an element (update the age of a student by name) public static void update (Document doc, String name, string age) throws Exception {NodeList nodes = doc. getElementsByTagName ("name"); for (int I = 0; I <nodes. getLength (); I ++) {Element nameE = (Element) nodes. item (I); if (nameE. getTextContent (). equals (name) {Node prNode = nameE. getParentNode (); NodeList stuAttributes = prNode. getChildNodes (); for (int j = 0; j <stuAttributes. getLength (); j ++) {Node stuAttribute = stuAttributes. item (j); if (stuAttribute. getNodeName (). equals ("age") {stuAttribute. setTextContent (age) ;}}} updateToXML (doc) ;}// delete an element (delete a student by name) public static void delete (Document doc, String name) throws Exception {// find the first student NodeList nodes = doc. getElementsByTagName ("name"); for (int I = 0; I <nodes. getLength (); I ++) {Node node = nodes. item (I); if (node. getTextContent (). equals (name) {Node prNode = node. getParentNode (); prNode. getParentNode (). removeChild (prNode) ;}}// update to XMLupdateToXML (doc);} // add a student to the XML file public static void add (Document doc) throws Exception {// create a new student node Element newStu = doc. createElement ("student"); newStu. setAttribute ("gender", "male"); Element newStu_name = doc. createElement ("name"); newStu_name.setTextContent ("James"); Element newStu_age = doc. createElement ("age"); newStu_age.setTextContent ("21"); Element newStu_intro = doc. createElement ("Introduction"); newStu_intro.setTextContent ("good"); newStu. appendChild (newStu_name); newStu. appendChild (newStu_age); newStu. appendChild (newStu_intro); // add the new student node to the root element doc. getDocumentElement (). appendChild (newStu); // update to XMLupdateToXML (doc);} // update to XMLprivate static void updateToXML (Document doc) throws TransformerFactoryConfigurationError, TransformerConfigurationException, transformerException {// Obtain the TransformerFactory object TransformerFactory transformerFactory = TransformerFactory. newInstance (); // Obtain a Transformer transformer = TransformerFactory from the transformerFactory object. newTransformer (); transformer. transform (new DOMSource (doc), new StreamResult ("src/com/pc/XML5.xml");} // query the information of a student (all of the first student in an hour) public static void read (Document doc) {NodeList nodes = doc. getElementsByTagName ("student"); // retrieve the first student Element stu1 = (Element) nodes. item (0); Element name = (Element) stu1.getElementsByTagName ("name "). item (0); System. out. println ("name:" + name. getTextContent () + "gender:" + stu1.getAttribute ("gender");} // traverse the XML file public static void list (Node node) {if (node. getNodeType () = node. ELEMENT_NODE) {System. out. println ("name:" + node. getNodeName ();} // Retrieve the node's subnode NodeList nodes = node. getChildNodes (); for (int I = 0; I <nodes. getLength (); I ++) {// display all subnodes Node n = nodes. item (I); list (n );}}}
The above is the content of the XML programming-DOM. For more information, see The PHP Chinese website (www.php1.cn )!