Component elements of XML

The document statement

  • XML document declaration format:
<? xml version="1.0" encoding="UTF-8"? >Copy the code
  1. The document declaration must be
    over;
  2. The document declaration must start at line 0, column 0 of the document;
  3. The document declares only two attributes:
1. Versioin: Specifies the XML document version. Must attribute, because we won't choose 1.1, only 1.0;
2. Encoding: Specifies the encoding of the current document. Optional property, default is UTF-8;
Copy the code

The element

  • Element element
<bean></bean>
Copy the code
  1. Elements are the most important part of an XML document,
  2. The structure of common elements consists of the start tag, element body, and end tag. Hello, everyone
  3. Element body: Element body can be an element or text, for example: Hello
  4. Empty element: An empty element has only a start tag, but no end tag, but the element must close itself, for example:
  5. Element naming:
1. Case sensitive
2. No Spaces, no colons:
3. It is not recommended to start with XML, XML, XML
Copy the code

A well-formatted XML document must have only one root element.

attribute

  • Property attribute
<bean id="" className="">
Copy the code
  1. An attribute is part of an element and must appear in the element’s opening tag
  2. Attribute definition format: attribute name = attribute value, where attribute value must use single or double citation
  3. An element can have 0 to N attributes, but an element cannot have attributes of the same name
  4. Attribute names cannot contain special characters such as Spaces or colons, and must start with a letter

annotation

XML comments, ending with “”. Comment content is ignored by the XML parser!

Escape character

Because many symbols are already used in XML document structures, you must use escape characters in element bodies or attribute values, such as “<“, “>”, “‘”, “” “, and” & “.

XML constraint

In XML technology, you can write a document that constrains the writing specification of an XML document, which is called an XML constraint. Common XML constraints: DTD, Schema

DTD constraints

What is a DTD

A Document Type Definition (DTD) is a Document Type Definition used to constrain XML documents. Specify the names of the elements in the XML document, the names and order of the child elements, the attributes of the elements, and so on.

DTD Key requirements

In development, we rarely write DTD constraint documents ourselves, rather we use the DTD constraint documents provided by the framework to write corresponding XML documents. Common frameworks use DTD constraints such as SpringMVC, MyBatis, etc.

Write XML using the provided DTD “bean.dtd”.

<? xml version="1.0" encoding="UTF-8"? > <! -- Chuanzhi Podcast DTD teaching example document. Emulating the Spring specification, developers must include docTypes if they want to use current DTD constraints on XML. Format: <! DOCTYPE beans SYSTEM"bean.dtd"> -- > <!ELEMENT beans (bean*,import*)> <! ELEMENTbean (property*)> <! ELEMENTproperty (#PCDATA)> <! ELEMENTimport (#PCDATA)> <! ATTLIST bean id CDATA #REQUIRED className CDATA #REQUIRED > <! ATTLIST property name CDATA #REQUIRED value CDATA #REQUIRED > <! ATTLISTimport resource CDATA #REQUIRED>
Copy the code

Case implementation

Complete XML content writing:


      
<! DOCTYPEbeans SYSTEM "bean.dtd">
<beans>
	<bean id="" className=""></bean>
	
	<bean id="" className="">
		<property name="" value=""></property>
		<property name="" value=""></property>
	</bean>
	
	<import resource=""></import>
	<import resource=""></import>

</beans>
Copy the code

Schema constraint

What is a Schema

Schema is the new XML document constraint; Schemas are much more powerful than DTDS and are a substitute for DTDS; Schema itself is also an XML document, but the extension of the Schema document is XSD, not XML. Schema is more powerful and has more sophisticated data types. Schema supports namespaces

Key Requirements of Schema

As with DTDS, XML documents are required to be written from schema constraint documents. Common frameworks that use Schema include: Spring, etc

Write XML documents by providing “bean-schema.xsd”

<? The XML version = "1.0" encoding = "utf-8"? > <! -- Chuangzhi Podcast Schema teaching example document. Emulating the Spring specification, if a developer needs to use the current Schema constraint on XML, the specified namespace must be included. The format is as follows:  <beans xmlns="http://www.itcast.cn/bean" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.itcast.cn/bean bean-schema.xsd" > --> <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.itcast.cn/bean" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.itcast.cn/bean" elementFormDefault="qualified"> <! --> <element name="beans"> <complexType> <choice minOccurs="0" maxOccurs="unbounded"> <element name="bean"> <complexType> <sequence minOccurs="0" maxOccurs="unbounded"> <element name="property"> <complexType> <attribute name="name" use="required"></attribute> <attribute name="value" use="required"></attribute> </complexType> </element> </sequence> <attribute name="id" use="required"></attribute> <attribute name="className" use="required"></attribute> </complexType> </element> <element name="import"> <complexType> <attribute name="resource" use="required"></attribute> </complexType> </element> </choice> </complexType> </element> </schema>Copy the code

Case implementation

Complete the XML content writing


      
<beans xmlns="http://www.itcast.cn/bean"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xsi:schemaLocation="http://www.itcast.cn/bean bean-schema.xsd"
>
	<bean id="" className=""></bean>
	<bean id="" className="">
		<property name="" value=""/>
		<property name="" value=""/>
	</bean>
	
	<import resource=""/>
	<import resource=""/>
</beans>
Copy the code

3.3 Namespaces (Syntax)

What is a namespace

Name conflicts occur when multiple Schema files are used in an XML document and elements with the same name are defined in those Schema files. This is like a Java file that uses import java.util.* and import java.sql.*. When using the Date class, it is not clear which package Date is in.

In general, namespaces are used to handle element and attribute name conflicts, the same purpose as packages in Java. If each element and attribute has its own namespace, there will be no name conflicts, just as each class has its own package, there will be no class name conflicts.

Constrain documents and XML relationships

The XML documents we write ourselves are subject to “custom Schema constraint documents.

The “custom Schema constraint document” is subject to the “official constraint Document” provided by W3C when the schema constraint specification was proposed.

Declaring a namespace

Default namespace:<xxx xmlns="">, using the < tag > explicit namespace:<xxx xmlns:The alias ="">, using < aliases: tags >Copy the code

XML parsing

Overview of XML Parsing

When data is stored in XML, we want to programmatically retrieve the content of the XML. If we use Java to learn IO knowledge can be completed, but you need very tedious operations to complete, and the development will encounter different problems (read only, read and write). People provide different parsers for different problems and submit corresponding parsers to make it easier for developers to manipulate XML.

Common parsing methods and parsers

  • There are three common parsing methods in development, as follows:

    • DOM: Requires the parser to load the entire XML Document into memory and parse it into a Document object.
Advantages: Retain the structural relationship between elements, so you can add, delete, change and check the operation.
Disadvantages: XML documents are too large and may overflow memory.
Copy the code
  • SAX: Is a faster, more efficient method. It scans the document line by line, parsing it as it goes. And in the event-driven manner of concrete parsing, each line, will trigger the corresponding event. (understand)
Advantages: Fast processing speed, can process large files
Disadvantages: read-only, resources will be freed after line by line.
Copy the code
  • PULL: The built-in XML parsing method of Android, similar to SAX. (understand)

Parsers: Are concrete implementations that are provided in different ways of parsing. Some parsers are too cumbersome to operate. For the convenience of developers, there are easy to operate parsing development packages.

Dom parsing principle and structure model

The XML DOM loads the entire XML Document into memory, generates a DOM tree, and gets a Document object from which you can manipulate the DOM.

The core concept in DOM is nodes. Elements, attributes, text, etc. in XML documents are nodes in DOM!

Use the API

DOM4J is a Java XML API with excellent performance, power, and ease of use. It outperforms Sun’s official DOM technology. Nowadays, more and more Java software are using DOM4J to read and write XML.

If you want to use DOM4J, you need to introduce the XPath-enabled JAR package dom4J-1.6.1.jar

DOM4J must load the XML Document to get the Document using the core class SaxReader, get the root element of the Document through the Document object, and then work on it.

Common apis are as follows:

  1. SaxReader object

    Read (...). Load the execution XML documentCopy the code
  2. The Document object

    GetRootElement () gets the root elementCopy the code
  3. The Element object

    Elements (...). Gets all child elements with the specified name. You can omit the name Element (...) Gets the first child element of the specified name. You can get the element name of the current element without specifying the name getName() attributeValue(...) Get the attribute value of the specified attribute name elementText(...) GetText () gets the text content of the current elementCopy the code

API Case Implementation

Writing an XML file:


      
<beans>
    <bean id="001" className="cn.itcast.demo.User">
        <property name="user" value="jacl"></property>
        <property name="user" value="rose"></property>
    </bean>

    <bean id="002" className="cn.itcast.demo.Admin">
        <property name="user" value="admin"></property>
        <property name="user" value="write"></property>
    </bean>
</beans>
Copy the code

Write code to parse XML:

public static void main(String[] args) throws Exception {
    SAXReader sax = new SAXReader();
    Document document = sax.read("beans.xml");

    Element elemRoot = document.getRootElement();
    List<Element>list = elemRoot.elements();
  
    for(Element element : list){
        String id =element.attributeValue("id");
        String className = element.attributeValue("className");
        System.out.println(id+""+className);

        List<Element>listElem = element.elements();
        for(Element elem : listElem){
            String name = elem.attributeValue("name");
            String value = elem.attributeValue("value");
            System.out.println(name+""+value); }}}Copy the code

XPath to parse the XML

  • XPath is a language for finding information in XML and HTML documents.
  • XPath is a W3C standard and the syntax is available through the W3CSchool documentation

Because DOM4J parses XML only one layer at a time, it is inconvenient to use when there are too many layers of AN XML file, and you can retrieve an element directly with XPATH

Xpath specific operations are supported using DOM4J

By default, Dom4j does not support xpath. If you want to use xpath with Dom4j, you need to introduce the XPath-supporting JAR package jaxEN-1.1.6.jar

Two methods are provided in Dom4J to support xpath

List<Node> selectNodes("Xpath expression")To obtain multiple nodes NodeselectSingleNode("Xpath expression")To get a nodeCopy the code

Xpath expressions are commonly used as queries

  • The first form of query

    /AAA/DDD/BBB: indicates a layer of BBBS under AAA and DDDCopy the code
  • Second query form

    //BBB: indicates the same name as this, indicating that as long as the name is BBB, getCopy the code
  • The third form of query

    /*: all elementsCopy the code
  • The fourth form of query

    BBB[1] : indicates the first BBB element. BBB[last()] : indicates the last BBB elementCopy the code
  • The fifth form of query

    //BBB[@id] : indicates that all BBB elements have id attributesCopy the code
  • Sixth form of query

    //BBB[@id='b1'] indicates that the element name is BBB, the id attribute above BBB, and the id attribute value is b1Copy the code

Case implementation

Writing an XML file:


      
<students>
	<student number="heima_0001">
		<name id="itcast">
			<xing>zhang</xing>
			<ming>three</ming>
		</name>
		<age>18</age>
		<sex>male</sex>
	</student>
	<student number="heima_0002">
		<name>jack</name>
		<age>18</age>
		<sex>female</sex>
	</student>
</students>
Copy the code

Write xpath code to parse an XML file:

public static void main(String[] args) throws Exception {
        SAXReader saxReader=new SAXReader();
        String path = Demo4jXpath.class.getClassLoader().getResource("student.xml").getFile();
        File file = new File(path);
        Document document=saxReader.read(file);

        //4. Query with xpath syntax
        // query all student tags
        List<Node> nodes = document.selectNodes("//student");
        for (Node node : nodes) {
            System.out.println(node);
        }

        System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");

        Select * from student; // select * from student
       nodes = document.selectNodes("//student/name");
        for (Node node : nodes) {
            System.out.println(node);
        }

        System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");

        Select * from student; // select * from student
        nodes = document.selectNodes("//student/name[@id]");
        for (Node node : nodes) {
            System.out.println(node);
        }
        System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
        Select * from student where id = itcast

        nodes = document.selectNodes("//student/name[@id='itcast']");
        for(Node node : nodes) { System.out.println(node); }}Copy the code