Database technology VI: XML

Posted by djrichwz on Fri, 17 Dec 2021 00:39:11 +0100

Basic introduction to XML

XML -- Extensible Markup Language.

XML features: extensible, tags are user-defined; The grammar is very strict.

  • The role of XML

Store data: usually, we store data in a database. However, if you want more portable data, you can store the data in an XML file.

Configuration file: it is more used as the configuration file of various technical frameworks.

Transmission in the network: the client can send data to the server in XML format, and the server then parses the data in XML format.

XML syntax

  • XML document declaration format

The document declaration must be closed and must be written on the first line.

Versionin: Specifies the XML document version, which is a required attribute, because 1.1 will not be selected, only 1.0 will be selected.

Encoding: Specifies the encoding of the current document. It is an optional attribute. The default value is utf-8.

<?xml version="1.0" encoding="UTF-8"?>
  • element

Element element: is the most important part of an XML document.

Naming rules:

  1. No spaces, no colons
  2. XML tag names are case sensitive
  3. XML must have one and only one root element
  4. XML must have and only have one root element, which is the parent element of all other elements. For example, the following users are the root element.

<?xml version="1.0" encoding="utf-8" ?>
<users>
</users>

The structure of a common element consists of a start tag, an element body, and an end tag.

<hello>hello everyone</hello>

Element body: the element body can be an element or text.

<hello>
    <a>Hello!</a>
</hello>

Empty element: an empty element has only a start tag and no end tag, but the element must close itself.

<close/>
  • attribute

  1. An attribute is part of an element and must appear in the element's start tag.
  2. Attribute definition format: attribute name = "attribute value", where attribute value must use single reference or double reference.
  3. An element can have 0 ~ N attributes, but an attribute with the same name cannot appear in an element.
  4. Property names cannot use special characters such as spaces and colons, and must begin with a letter.

<bean id="1" class="x"></bean>
  • notes

XML Notes for:
with <!-- Start with --> end
 The content of the comment will be deleted XML Parser ignored.
  • Using XML to describe data

<?xml version="1.0" encoding="UTF-8" ?>
<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>

XML constraints

In XML technology, you can write a document to restrict the writing specification of an XML document, which is called XML constraint.

Common XML constraints: DTD, Schema.

"User programmer" reads XML constraint documents and writes XML documents; The software framework writes XML constraint documents and parses XML documents; XML constraint document specifies the writing rules of XML document.

  • DTD constraints

DTD - Document Type De "nition", a document type definition used to constrain XML documents. Specify the name of elements, the name and order of child elements, and the attributes of elements in XML documents.

Write the corresponding XML document through the DTD constraint document provided by the framework. DTD constraints used by common frameworks include struts 2, hibernate, etc.

There are two ways to import DTD documents into XML documents:

  • Internal DTD -- defines constraint rules in an XML document.
  • External DTD -- define the rules of the constraint in the external DTD file.
    Create constraint file student dtd

<!ELEMENT students (student+) >
<!ELEMENT student (name,age,sex)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT sex (#PCDATA)>
<!ATTLIST student number ID #REQUIRED>
<!--
ELEMENT: Used to define elements
students (student+) : Representative root element must be <students>
student+ : At least one of the root Tags student Child element, + Represents at least one
student (name,age,sex): student Child elements contained in Tags,Appear in order
#PCDATA: normal text content
ATTLIST: Used to define attributes
student number ID #REQUIRED
student One of the child elements ID Properties are called number,It is required
ID: Unique values can only start with letters or underscores
-->

Introduce constraint document to student xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE students SYSTEM "student.dtd">
<students>
    <student number="s1">
        <name>Blair</name>
        <age>22</age>
        <sex>male</sex>
    </student>
    <student number="s2">
        <name>Zhang Renmin</name>
        <age>55</age>
        <sex>male</sex>
    </student>
</students>
  • Schema constraints

Schema is a new XML document constraint, which is much more powerful than DTD and a substitute for DTD.

Schema itself is also an xml document, but the extension of schema document is xsd, not xml.

Schema is more powerful, with a variety of simple and complex data types built in.

Schema supports namespaces (multiple constraint documents can be introduced into one XML).

Namespace: refers to an environment from which the label is defined.

Create student xsd
xmlns -- indicates what is the default namespace for this document

xmlns:xsd -- represents the source of definitions such as data types

targetNamespace -- indicates the namespace from which the element to be defined in the document comes

elementFormDefault -- indicates that each element of the XML file is required to have a namespace specified

<?xml version="1.0"?>
<xsd:schema xmlns="http://www.xxx.com/xml"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.xxx.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="xxx_\d{4}"/>
        </xsd:restriction>
    </xsd:simpleType>
</xsd:schema>

XML introduces Schema constraints
First, check the Schema document and find the root element in student.xml XML.

Use the xmlns directive to declare which namespace the root element comes from.

Introduce the W3C standard namespace in xmlns:xsi and copy it.

Use schemaLocation to specify which XSD file the imported namespace corresponds to. There are two values: the first is the namespace and the second is the path of the XSD file.

<?xml version="1.0" encoding="UTF-8" ?>
<students xmlns="http://www.xxx.com/xml"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://www.xxx.com/xml student.xsd">
    <student number="xxx_1234">
        <name>Xiao Hong Zhang</name>
        <age>25</age>
        <sex>female</sex>
    </student>
    <student number="xxx_0000">
        <name>Li Dabing</name>
        <age>20</age>
        <sex>male</sex>
    </student>
</students>

XML parsing

After storing the data in XML, you want to get the content of XML through the program. If you use Java, the IO stream can be completed, but it requires very cumbersome operations, and different problems (read-only and read-write) will be encountered in development. Therefore, in order to facilitate developers to operate XML, you need to provide different parsing methods and corresponding parsers for different problems.

Parser: it is the specific implementation provided according to different parsing methods. Some parsers are too cumbersome to operate. In order to facilitate developers, an easy to operate parsing development package is provided.

  • XML parsing method

DOM -- Document Object Model: it belongs to document driven. The parser is required to load the entire XML document into memory, parse it into a document object, establish a DOM tree, and generate each Node object on the DOM tree.

  • Advantages: the structural relationship between elements is preserved, so you can add, delete, modify and query. You can read and modify any part of the DOM tree.
  • Disadvantages: the XML document is too large and memory overflow may occur.

SAX -- Simple API for XML: it is event driven and is a faster and more effective method. It scans the document line by line and parses it as it scans. It is analyzed in an event driven manner. Each line will trigger the corresponding event.

  • Advantages: it occupies less memory and has fast processing speed, and can process large files.

  • Disadvantages: it can only be accessed and read sequentially and cannot be modified. We need to build our own XML object model, which increases the difficulty of development.

  • Common parsing of XML

JAXP: the parser provided by Sun company supports Dom and SAX. Dom4j: an excellent parser. Dom4j is an easy-to-use, open source library for XML, XPath and XSLT. It is applied to the Java platform, adopts the Java collection framework, and fully supports DOM, SAX and JAXP. Jsup: jsup is a Java HTML parser that can also parse XML. PULL: Android's built-in XML parsing method, similar to SAX.

  • Use of Dom4j

First, import the dom4j JAR package.

Use the core class SaxReader to load the XML Document to obtain the Document, obtain the root element of the Document through the Document object, and then you can operate.

SaxReader object: read(...) loads and executes an XML document

Document object: getRootElement() gets the root element

Element object:

  • elements(...) gets all child elements of the specified name. You may not specify a name
  • element(...) gets the first child element of the specified name. You may not specify a name
  • getName() gets the element name of the current element
  • attributeValue(...) gets the attribute value of the specified attribute name
  • elementText(...) gets the text value of the child element with the specified name
  • getText() gets the text content of the current element

1. Use schema constraints to write user xsd

<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns="http://www.xxx.com/xml"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.xxx.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="run"/>
            <xsd:enumeration value="Swimming"/>
            <xsd:enumeration value="read a book"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:simpleType name="numberType">
        <xsd:restriction base="xsd:string">
            <xsd:pattern value="\d"/>
        </xsd:restriction>
    </xsd:simpleType>
</xsd:schema>

2. Introduce constraints to write user xml

<?xml version="1.0" encoding="UTF-8" ?>
<users xmlns="http://www.xxx.com/xml"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.xxx.com/xml user.xsd">
    <user id="1">
        <name>Zhang San</name>
        <age>20</age>
        <hobby>run</hobby>
    </user>
    <user id="2">
        <name>Blair</name>
        <age>10</age>
        <hobby>Swimming</hobby>
    </user>
    <user id="3">
        <name>He Xiaoming</name>
        <age>40</age>
        <hobby>read a book</hobby>
    </user>
</users>

3. Read XML

public class TestDOM4j {
    // Get all element names (labels) in the XML file
    @Test
    public void test1() throws DocumentException {
        // Get XML parsing object
        SAXReader reader = new SAXReader();
        // Parsing XML to get document object document
        Document document = reader.read("user.xml");
        // Get root element
        Element rootElement = document.getRootElement();
        // Get root element name
        System.out.println(rootElement.getName());
        // Gets the label under the root element
        List<Element> elements = rootElement.elements();
        for (Element element : elements) {
            System.out.println("Child nodes under the root label: " + element.getName());
            List<Element> eList = element.elements();
            for (Element e : eList) {
                System.out.println("user Child nodes under label" + e.getName());
            }
            break;
        }
    }

    /**
     * Get specific node content
     */
    @Test
    public void test2() throws DocumentException {
        // Create XML document parsing object
        SAXReader sr = new SAXReader();
        // Read XML and get document object
        Document document = sr.read("src\\user.xml");
        // Get root node
        Element rootElement = document.getRootElement();
        // Get all child nodes of the current node
        List<Element> elements = rootElement.elements();
        // Get the first child node
        Element user = elements.get(0);
        // Get all information
        String id = user.attributeValue("id");
        String name = user.elementText("name");
        String age = user.elementText("age");
        // Use getText to get the text content of the current element
        String hobby = user.element("hobby").getText();
        System.out.println(id+" "+name+" "+age+" "+hobby);
    }
}
  • Reading XML in XPath mode

XPath is a language for finding information in XML documents. You can use XPath to find the content in XML.

Because DOM4J can only parse XML layer by layer, it is inconvenient to use when there are too many layers of XML files. You can directly obtain an element in combination with XPath.

Based on the imported DOM 4J JAR package, you need to import the jaxen JAR package additionally.

Introduction to basic XPath syntax
There are several main forms of XPath supported operations using Dom4j:

  • /AAA/DDD/BBB select all child elements of AAA and DDD
  • //Bbbselect all BBB elements
  • //*Select all elements
  • BBB[1] select the first child element of BBB, and BBB[last()] means select the last child element of BBB
  • //BBB[@id] select the BBB element with id attribute
  • //BBB[@id='b1 '] select the BBB element with attribute ID and its value of' B1 '

API introduction
Select single node (query) finds a node that matches the XPath query. The parameter is the XPath query string.

selectNodes(query) obtains all nodes that meet XPath under the XML root node, and the parameter is the XPath query string.

Node object

  • XPath reads XML

<?xml version="1.0" encoding="UTF-8" ?>
<bookstore>
    <book id="book1">
        <name>Journey to the West</name>
        <author>Wu Chengen</author>
        <price>99</price>
    </book>
    <book id="book2">
        <name>The Dream of Red Mansion</name>
        <author>Cao Xueqin</author>
        <price>69</price>
    </book>
    <book id="book3">
        <name>Java Programming thought</name>
        <author>Eckel</author>
        <price>59</price>
    </book>
</bookstore>

/*
 * Use the selectSingleNode method to query the content in the specified node
 **/
@Test
public void test1() throws DocumentException {
    // Create parser object
    SAXReader sr = new SAXReader();
    // Get document object
    Document document = sr.read("book.xml");
    // Call the selectSingleNode() method to get the name node object
    Node node1 = document.selectSingleNode("/bookstore/book/name");
    System.out.println("node: " + node1.getName());
    System.out.println("title: " + node1.getText());
    // Get the name of the second book
    Node node2 = document.selectSingleNode("/bookstore/book[2]/name");
    System.out.println("The title of the second book is: " + node2.getText());
}
/*
 * Use the selectSingleNode method to obtain the attribute value or the node corresponding to the attribute value
 **/
@Test
public void test2() throws DocumentException {
    // Create parser object
    SAXReader sr = new SAXReader();
    // Get document object
    Document document = sr.read("book.xml");
    // Gets the value of the id attribute of the first book node
    Node node1 = document.selectSingleNode("/bookstore/book/attribute::id");
    System.out.println("first book of id Value is: " + node1.getText());
    // Gets the value of the id attribute of the last book node
    Node node2 = document.selectSingleNode("/bookstore/book[last()]/attribute::id");
    System.out.println("the last one book Nodal id Value is: " + node2.getText());
    // Get the book name with id attribute value of book2
    Node node3 = document.selectSingleNode("/bookstore/book[@id='book2']");
    String name = node3.selectSingleNode("name").getText();
    System.out.println("id by book2 The title of my book is: " + name);
}
/*
 * Use the selectNodes() method to get all nodes with corresponding names
 **/
@Test
public void test3() throws DocumentException {
    // Create parser object
    SAXReader sr = new SAXReader();
    // Get document object
    Document document = sr.read("book.xml");
    // Print get all nodes
    List<Node> list = document.selectNodes("//*");
    for (Node node : list) {
        System.out.println("Node name: " + node.getName());
    }
    // Get all book titles
    List<Node> names = document.selectNodes("//name");
    for (Node name : names) {
        System.out.println(name.getText());
    }
    // Gets all the contents of the node with the specified id value of book1
    List<Node> book1 = document.selectNodes("/bookstore/book[@id='book1']//*");
    for (Node node : book1) {
        System.out.println(node.getName() + " = " + node.getText());
    }
}

JDBC custom XML

  • Define XML configuration file

Create a custom XML file jdbc-con "g.xml" to save the database connection information

<?xml version="1.0" encoding="UTF-8" ?>
<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">root</property>
</jdbc>
  • Writing tool classes using XPath

public class JDBCUtils {
    // Define string variables to record the information required to obtain the connection
    public static String DRIVERNAME;
    public static String URL;
    public static String USER;
    public static String PASSWORD;
    // Static code block
    static {
        try {
            // Use XPath to read configuration information in XML
            SAXReader sr = new SAXReader();
            Document document = sr.read("jdbc-config.xml");
            DRIVERNAME = document.selectSingleNode("/jdbc/property[@name='driverClass']").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 driver
            Class.forName(DRIVERNAME);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // Gets the static method of the connection
    public static Connection getConnection(){
        try {
            // Get connection object
            Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);
            // Return connection object
            return connection;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
}
  • Test tool class

// Get the names of all employees
public static void main(String[] args) {
    try {
        // Get connection
        Connection connection = JDBCUtils.getConnection();
        // Get statement and execute SQL
        Statement statement = connection.createStatement();
        String sql = "select * from employee";
        // Processing result set
        ResultSet resultSet = statement.executeQuery(sql);
        while(resultSet.next()){
            String ename = resultSet.getString("ename");
            System.out.println(ename);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

Topics: Database xml