Introduction to XML

An overview of the

XML Extensible Markup Language

  • The W3C released version 1.0 in February 1998, followed by version 1.1 in February 2004, but since version 1.1 was not retro-compatible with version 1.0, 1.1 was not used. Meanwhile, in February 2004, the W3C released version 1.0, version 3.

The characteristics of

  • Extensible, tags are custom
  • Grammar is very strict

The role of XML

function instructions
Store the data Typically, we store data in a database. However, if you want to be more portable, you can store the data in XML files
The configuration file Used as a configuration file for various technical frameworks (most)
Transmission over the network The client can send data to the server in XML format. The server receives the DATA in XML format and parses it

The syntax of XML

XML document declaration format

  • Document declaration must be end;
  • The document declaration must be in the first line;

1) Syntax format:

2) Attribute Description:

  • Versioin: Specifies the XML document version. Must attribute, because we won’t choose 1.1, only 1.0;
  • Encoding: Specifies the encoding of the current document. Optional property, default is UTF-8;

The element

  • Element: Is the most important part of an XML document
  • Naming rules for elements
    1. You can’t use Spaces, you can’t use colons
    2. XML label names are case sensitive
    3. XML must have one and only one root element
  • Syntax format:

<users><users>

1) XML must have one and only one root element, which is the parent of all other elements. For example, in the following example users is the root element:


       
<users> 

</users>
Copy the code

2) Structure of common elements: start tag, element body and end tag.

<hello>Hello, everyone</hello>
Copy the code

3) Element body: Element body can be an element or text

<hello> 
    <a>hello</a> 
</hello>
Copy the code

4) Empty element: An empty element has only a start tag, but no end tag, but the element must close itself

<close/>
Copy the code

attribute

<bean id="" class=""> </bean>
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!

Use XML to describe the data in a data table


      
<employees> 
    <employee eid="2"> 
        <ename>Lin daiyu</ename> 
        <age>20</age> 
        <sex>female</sex> 
        <salary>5000</salary> 
        <empdate>2019-03-14</empdate> 
    </employee> 
    
    <employee eid="3"> 
        <ename>Du fu</ename> 
        <age>40</age> 
        <sex>male</sex> 
        <salary>15000</salary> 
        <empdate>2010-01-01</empdate> 
    </employee> 
</employees>
Copy the code

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
  • There are only two things you need to know as a programmer
    • Will read
    • Will introduce
    • You don’t have to write it yourself

DTD constraints

  • 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.

Write a DTD

  • In development, we will not write DTD constraint documents ourselves
  • In most cases, we write corresponding XML documents constrained by a DTD provided by the framework. Common frameworks use DTD constraints such as Struts2, Hibernate, etc.

Create the constraint file Student.DTD

<! ELEMENTstudents (student+) > 
    <! ELEMENTstudent (name.age.sex) > 
    <! ELEMENTname (#PCDATA) > 
    <! ELEMENTage (#PCDATA) > 
    <! ELEMENTsex (#PCDATA) > 
    <! ATTLISTstudent number ID #REQUIRED> 

<! Students (student+) : indicates that the root ELEMENT must be <students> student+ : There is at least one student child element in the root tag, with + representing at least one student (name,age,sex): Student child element contained in the student tag, in order #PCDATA: is plain text content ATTLIST: Student number ID #REQUIRED The student child element has an ID attribute called number, which is a mandatory ID: the only value must start with a letter or underscore -->
Copy the code

The introduction of the DTD

  • Introduce DTD documents into XML documents in two ways
    • Internal DTD: Constraint rules are defined in XML documents
    • External DTD: Define the rules of the constraints in an external DTD file
      • Local:
      • Network:
  • student.xml

       
<! DOCTYPEstudents SYSTEM "student.dtd"> 
<students> 

    <student number="s1"> 
        <name>x-bin</name> 
        <age>22</age> 
        <sex>male</sex>
    </student> 
        
    <student number="s2"> 
        <name>Wide kun</name> 
        <age>55</age> 
        <sex>male</sex> 
    </student> 

</students>
Copy the code

Schema constraint

What is a Schema

  1. Schema is the new XML document constraint, much more powerful than DTDS and a replacement for DTDS;
  2. Schema itself is also an XML document, but the extension of the Schema document is XSD, not XML.
  3. Schema is more powerful, with multiple built-in simple and complex data types
  4. Schema supports namespaces (multiple constraint documents can be imported into an XML)

Schema constraint Example

student.xsd


       
<xsd:schema xmlns="http://www.lagou.com/xml" 
            xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
            targetNamespace="http://www.lagou.com/xml" 
elementFormDefault="qualified"> 

    <xsd:element name="students" type="studentsType"/> 
    <xsd:complexType name="studentsType"> 
        <xsd:sequence> 
            <xsd:element name="student" type="studentType" minOccurs="0" maxOccurs="unbounded"/> 
        </xsd:sequence> 
    </xsd:complexType> 
    <xsd:complexType name="studentType"> 
        <xsd:sequence> 
            <xsd:element name="name" type="xsd:string"/> 
            <xsd:element name="age" type="ageType" /> 
            <xsd:element name="sex" type="sexType" /> 
        </xsd:sequence> 
        <xsd:attribute name="number" type="numberType" use="required"/> 
    </xsd:complexType> 
    <xsd:simpleType name="sexType"> 
        <xsd:restriction base="xsd:string"> 
            <xsd:enumeration value="male"/> 
            <xsd:enumeration value="female"/> 
        </xsd:restriction> 
    </xsd:simpleType> 
    <xsd:simpleType name="ageType"> 
        <xsd:restriction base="xsd:integer"> 
            <xsd:minInclusive value="0"/> 
            <xsd:maxInclusive value="200"/> 
        </xsd:restriction> 
    </xsd:simpleType> 
    <xsd:simpleType name="numberType">
        <xsd:restriction base="xsd:string"> 
            <xsd:pattern value="hehe_\d{4}"/> 
        </xsd:restriction> 
    </xsd:simpleType> 
</xsd:schema>
Copy the code

The root element of XmlSchema:

XML introduces Schema constraints

Steps to introduce schema constraints in XML:

1) Look at the Schema document, find the root element, and write it in XML


       
<students> 

</students>
Copy the code

2) which namespace the root element comes from. Declare using the XMLNS directive


       
<students 
    xmlns="http://www.lagou.com/xml"\ >

</students>
Copy the code

3) Introduce the W3C standard namespace and copy it


       
<students 
    xmlns="http://www.lagou.com/xml" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\ >
</students>
Copy the code

4) Which XSD file does the imported namespace correspond to? SchemaLocation is used to specify two values: the first is the namespace and the second is the path to the XSD file


       
<students 
    xmlns="http://www.lagou.com/xml" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.lagou.com/xml student.xsd"\ >
</students>
Copy the code

5) Namespaces

Refers to the environment from which the label is defined.Copy the code

6) student.xml


       
<students 
    xmlns="http://www.lagou.com/xml" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.lagou.com/xml student.xsd"\ > 
    <student number="hehe_1234"> 
        <name>Zhang, one million</name> 
        <age>25</age> 
        <sex>female</sex> 
    </student> 

    <student number="hehe_0000"> 
        <name>x-bin</name> 
        <age>20</age> 
        <sex>male</sex> 
    </student> 
</students>
Copy the code

XML parsing

Analytical overview

  • 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.

XML parsing

There are two 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.
  • 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: Less memory, fast processing speed, can process large files
    • Disadvantages: read-only, resources will be freed after line by line.

A common parser for XML

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

  • JAXP: A parser from Sun that supports both DOM and SAX ideas
  • DOM4J: An excellent parser, DOM4J is an easy-to-use, open source library for XML, XPath, and XSLT. It works on the Java platform, using the Java Collections framework and fully supporting DOM, SAX, and JAXP.
  • Jsoup: Jsoup is a Java HTML parser that can also parse XML
  • PULL: The built-in XML parsing method of Android, similar to SAX.

The use of dom4j

Import the JAR package

The API is introduced

Use the core class SaxReader to load the XML Document to get the Document, use the Document object to get the root element of the Document, and then manipulate the common API as follows:

  • SaxReader object
    • Read (…). Load the execution XML document
  • The Document object
    • GetRootElement () gets the root element
  • 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 omit the name
    • GetName () gets the element name of the current element
    • AttributeValue (…). Gets the property value of the specified property name
    • ElementText (…). Gets the text value of the named child element
    • GetText () gets the text content of the current element

Preparing the XML file

Write user.xsd schema constraints


       
<xsd:schema xmlns="http://www.lagou.com/xml" 
            xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
            targetNamespace="http://www.lagou.com/xml" 
elementFormDefault="qualified"> 

    <xsd:element name="users" type="usersType"/> 
    <xsd:complexType name="usersType"> 
        <xsd:sequence> 
            <xsd:element name="user" type="userType" minOccurs="0" maxOccurs="unbounded"/> 
        </xsd:sequence> 
    </xsd:complexType> 

    <xsd:complexType name="userType"> 
        <xsd:sequence> 
            <xsd:element name="name" type="xsd:string"/> 
            <xsd:element name="age" type="ageType" /> 
            <xsd:element name="hobby" type="hobbyType" /> 
        </xsd:sequence> 
        <xsd:attribute name="id" type="numberType" use="required"/> 
    </xsd:complexType> 

    <xsd:simpleType name="ageType"> 
        <xsd:restriction base="xsd:integer"> 
            <xsd:minInclusive value="0"/> 
            <xsd:maxInclusive value="100"/> 
        </xsd:restriction> 
    </xsd:simpleType> 

    <xsd:simpleType name="hobbyType"> 
        <xsd:restriction base="xsd:string"> 
            <xsd:enumeration value="Smoking"/> 
            <xsd:enumeration value="Drinking"/> 
            <xsd:enumeration value="Permed hair"/> 
        </xsd:restriction> 
    </xsd:simpleType> 

    <xsd:simpleType name="numberType"> 
        <xsd:restriction base="xsd:string"> 
            <xsd:pattern value="\d"/> 
        </xsd:restriction> 
    </xsd:simpleType> 

</xsd:schema>
Copy the code

Write user.xml to introduce constraints


       
<users 
        xmlns="http://www.lagou.com/xml" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://www.lagou.com/xml user.xsd" 
> 
    <user id="1"> 
        <name>Zhang, one million</name> 
        <age>20</age>
        <hobby>smoking</hobby> 
    </user> 

    <user id="2"> 
        <name>Yu Qian</name> 
        <age>50</age> 
        <hobby>drinking</hobby> 
    </user> 

    <user id="3"> 
        <name>Liu can</name> 
        <age>40</age> 
        <hobby>Permed hair</hobby> 
    </user> 
</users> 
Copy the code

Reads the XML

public class TestDOM4j { 

    // Get all the element names (tags) in the XML file
    @Test 
    public void test1(a) throws DocumentException { 

        //1. Get the XML parsing object
        SAXReader reader = new SAXReader(); 

        //2. Parse the XML to obtain the document object document
        Document document = reader.read("H:\\jdbc_work\\xml_task03\\src\\com\\lagou\\xml03\\user.xml"); 

        //3. Get the root element
        Element rootElement = document.getRootElement(); 

        // Get the root element name
        System.out.println(rootElement.getName()); 

        // Get the tag under the root element
        List<Element> elements = rootElement.elements(); 
        for (Element element : elements) { 
            System.out.println("Children under the root tag:" + element.getName()); 

            List<Element> eList = element.elements(); 
            for (Element e : eList) { 
                System.out.println("Child nodes under the User tag" + e.getName()); 
            }

            break; }}/** * get all the information about zhang Baiwan */ 
    @Test支那public void test2(a) throws DocumentException { 
        //1. Create an XML document parsing object
        SAXReader sr = new SAXReader(); 

        //2. Read XML to get document object
        Document document = sr.read("src\\com\\lagou\\xml02\\user.xml"); 

        3. Obtain the root node
        Element rootElement = document.getRootElement(); 

        //4. Get all children of the current node
        List<Element> elements = rootElement.elements(); 

        //5. Get the first child
        Element user = elements.get(0); 

        //6. Get all the information
        String id = user.attributeValue("id"); 
        String name = user.elementText("name"); 
        String age = user.elementText("age"); 
        // Get the text content of the current element using getText
        String hobby = user.element("hobby").getText(); 

        / / print
        System.out.println(id+"" + name +"" + age +""+ hobby); }}Copy the code

XML is read in xpath mode

Xpath is introduced

  • XPath is a language for finding information in XML documents. You can use xpath to find content in XML.
  • The benefits of XPath
    • 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

Basic syntax of XPath

  • Several major forms of xpath operations are supported using DOM4J
grammar instructions
/AAA/DDD/BBB It’s layer by layer, AAA under DDD and BBB under DDD
//BBB It’s the same name as this, it’s the same name as BBB
/ / * All the elements
BBB[1], BBB[last()] The first represents the first BBB element, and the second represents the last BBB element
//BBB[@id] As long as the BBB element has an ID attribute on it, it gets
//BBB[@id=’b1′] Indicates that the element name is BBB, the id attribute is on BBB, and the id attribute value is B1

The API is introduced

Common methods:

  • SelectSingleNode (query) : Finds a node that matches an XPath query.
    • The argument is an Xpath query string.
  • SelectNodes (query) : Gets all xPath-satisfied nodes under the XML root node;
    • The argument is an Xpath query string.
  • Node: Node object

JDBC custom XML

Defining a configuration file

1) Create a custom XML file to save database connection information

jdbc-config.xml


       
<jdbc>
    <property name="driverClass">com.mysql.jdbc.Driver</property> 
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/db5? characterEncoding=UTF-8</property> 
    <property name="user">root</property> 
    <property name="password">123456</property> 
</jdbc>
Copy the code

Writing utility classes (configuration)

2) Write utility classes and use xpath to read database information

public class JDBCUtils { 

    //1. Define a string variable to record the information needed to obtain the connection
    public static String DRIVERNAME; 
    public static String URL; 
    public static String USER; 
    public static String PASSWORD; 

    //2. Static code blocks
    static { 
        try {
            // Use xpath to read configuration information in XML
            SAXReader sr = new SAXReader(); 
            Document document = sr.read("H:\\workspace01\\JDBC_day02\\src\\com\\lagou\\xml03\\jdbc-config.xml"); 

            Node node = document.selectSingleNode("/jdbc/property[@name='driverClass']"); 
            //System.out.println(node.getText()); 
            DRIVERNAME = node.getText(); 
            URL = document.selectSingleNode("/jdbc/property[@name='jdbcUrl']").getText(); 
            USER = document.selectSingleNode("/jdbc/property[@name='user']").getText(); 
            PASSWORD = document.selectSingleNode("/jdbc/property[@name='password']").getText(); 

            // Register the driver
            Class.forName(DRIVERNAME); 
        } catch(Exception e) { e.printStackTrace(); }}//3. Static method to get the connection
    public static Connection getConnection(a){ 

        try {
            // Get the connection object
            Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);

            // Return the connection object
            return connection; 
        } catch (SQLException e) { 
            e.printStackTrace(); 
            return null; }}}Copy the code

Test tool class

3) Test: Get the names of all employees

// Get the names of all employees
public static void main(String[] args) { 

    try {
        //1. Obtain the connection
        Connection connection = JDBCUtils.getConnection(); 

        //2. Obtain statement and execute SQL
        Statement statement = connection.createStatement(); 
        String sql = "select * from employee"; 

        //3. Process the result set
        ResultSet resultSet = statement.executeQuery(sql); 
        while(resultSet.next()){ 
            String ename = resultSet.getString("ename"); System.out.println(ename); }}catch(SQLException e) { e.printStackTrace(); }}Copy the code