Home Backend Development XML/RSS Tutorial Take you to a deeper understanding of XML

Take you to a deeper understanding of XML

Apr 26, 2017 am 10:12 AM

1. XML: extensible markup language version="1.0"

  • ##Extensible: all tags are customized

  • Function: Data storage

    • Configuration file

    • Data transmission

  • Differences between html and xml

    • ##HTML syntax is loose, xml syntax is strict
    • html is used for page display, xml is used for data Storage
    • All tags in Html are predefined, and all tags in xml are customized
    2. xml syntax:

    Document declaration
    • version version number fixed value 1.0
    • encoding specified document The default value of the code table is iso-8859-1
    • standalone. Specify whether the document is independent yes or no
    • must be written in the xml document. One line of
    • is written as:
    • Attributes
    Tags in element xml document
    • Element names are case-sensitive
    • Numbers cannot begin
    • There must be and can only be one root element in the document
    • The element needs to be closed correctly

      elements need to be nested correctly
    • element names must comply with
    text
  • ##
    • Escape characters>
    • The data inside CDATA will be displayed as it is
    • Attributes
  • Attribute values ​​must be enclosed in quotation marks. Both single and double quotation marks are OK
    • Comments
  • #
    • Processing instructions: Basically no need now
    • 3. XML constraints

Constraints are the writing rules of xml

  • Classification of constraints:

  • Import xsd constraint document
    • Write root tag

    • Introducing the instance namespace xmlns:xsi="www.w3.org/2001/XMLSchema-instance"

    • ##Introducing the namespace xsi:schemaLocation="www.itcast.cn/ xml student.xsd"

    • Introducing the default namespace

    • student.xsd

    • student .xml

    • <?xml version="1.0"?>
      <xsd:schema xmlns="www.itheima.cn/xml"
              xmlns:xsd="www.w3.org/2001/XMLSchema"
              targetNamespace="www.itheima.cn/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="256"/>
              </xsd:restriction>
          </xsd:simpleType>
          <xsd:simpleType name="numberType">
              <xsd:restriction base="xsd:string">
                  <xsd:pattern value="itheima_\d{4}"/>
              </xsd:restriction>
          </xsd:simpleType>
      </xsd:schema>
      <?xml version="1.0" encoding="UTF-8" ?>
      Copy after login
    • <students
          xmlns="www.itheima.cn/xml"
          xsi:schemaLocation="www.itheima.cn/xml student.xsd"
          xmlns:xsi="www.w3.org/2001/XMLSchema-instance"
      >
          <student number="itheima_1001">
              <name>asfd</name>
              <age>12</age>
              <sex>male</sex>
          </student>
      </students>
      <students
          xmlns:itheima="www.itheima.cn/xml"
          xsi:schemaLocation="www.itheima.cn/xml student.xsd"
          xmlns:xsi="www.w3.org/2001/XMLSchema-instance"
      >
          <itheima:student number="itheima_1001">
              <itheima:name>asfd</itheima:name>
              <itheima:age>12</itheima:age>
              <theima:sex>male</itheima:sex>
          </itheima:student>
      </itheima:students>
      Copy after login
    • Internal dtd defines dtd inside xml
    • External dtd defines dtd in external file

    • Student.dtd

    • student.xml

    • Local dtd file

    • Network dtd file

    • <!ELEMENT students (student*) >
      <!ELEMENT student (name,age,sex)>
      <!ELEMENT name (#PCDATA)>
      <!ELEMENT age (#PCDATA)>
      <!ELEMENT sex (#PCDATA)>
      <!ATTLIST student number ID #REQUIRED> 唯一的,必须的
      <?xml version="1.0" encoding="UTF-8" ?>
      <!DOCTYPE students SYSTEM  "student.dtd">
      <students>
          <student number="s0001" >
              <name>zs</name>
              <age>abc</age>
              <sex>yao</sex>
          </student>
      </students>
      Copy after login
    • dtd The constraints are not strict
    • schema

    • ##4. XML parsing

    parsing XML can be done:

  • If xml is used as a configuration file: read

    • If xml is used as a transfer file: write, read
    • XML parsing ideas:

  • Advantages: Does not occupy memory, fast

    • Disadvantages: Can only read, cannot write back
    • Advantages: Because a dom tree will be formed in the memory, the dom tree can be added, deleted, modified, and checked
    • Disadvantages: The DOM tree takes up a lot of memory and the parsing speed is slow
    • Document Element Text Attribute Comment
    • DOM: Load the document to memory, forming a DOM tree (document object), encapsulating various components of the document into some objects
    • SAX: read line by line, event-driven
    • Commonly used parsers for xml

  • define a rule

    • Usage method
    • Usage steps
    • XPath:
    • public classTestXPath2 {
            @Test
            publicvoidtest()throwsException{
                  SAXReaderread= new SAXReader();
                  Documentdocument= read.read("src/Dom4jTest.xml");
                  Listnodes= document.selectNodes("/bookstore//book/title");
                  for(inti= 0;i< nodes.size();i++) {
                        Nodenode= (Node)nodes.get(i);
                        System.out.println(node.getText());
                  }
            }
      }
      Copy after login
    • selectSingleNode()

    • selectNodes()
    • Note: To import the package jaxen...jar
    • 创建解析器 SAXReader reader = new SAXReader()

    • 解析xml 获得document对象 Document document = reader.read(url)

    • // nodename 选取此节点。

    • // / 从根节点选取。

    • // // 从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置。

    • // .. 选取当前节点的父节点。

    • // @ 选取属性。

    • // [@属性名] 属性过滤

    • // [标签名] 子元素过滤

    • @Test

      //遍历所有元素节点

    •  publicvoidtest2()throwsException{
                  //创建一个xml解析对象
                  SAXReaderreader= new SAXReader();
                  //把xml文档加载到document对象中
                  Documentdocument= reader.read("src/Book.xml");
                  Elementroot= document.getRootElement();
                  treeWalk(root);
            }
            
            privatevoidtreeWalk(Elementele){
                  //输出当前节点的名字
                  System.out.println(ele.getName());
                  //ele.nodeCount()得到当前节点的所有子节点的数量
                  for(inti= 0;i<ele.nodeCount();i++){
                        //取出下标为i的节点
                        Nodenode= ele.node(i);
                        //判断当前节点是否为标签
                        if(nodeinstanceofElement){
                              //把node强转为标签(Element)
                              treeWalk((Element)node);
                        }
                  }
            }
      }
      Copy after login
    • public classTestDom4j {
            @Test
            publicvoidtest1()throwsException{
                  //创建一个xml解析对象
                  SAXReaderreader= new SAXReader();
                  //把xml文档加载到document对象中
                  Documentdocument= reader.read("src/Book.xml");
                  Elementroot= document.getRootElement();
      //          Element bookNode = root.element("书");
      //          System.out.println(bookNode.getName());
                  //得到当前节点所有的子节点
                  Listlist= root.elements();
                  //得到第二本书对象
                  ElementsecondBook= (Element)list.get(1);
                  //得到当前节点的文本内容
                  Stringname= secondBook.element("书名").getText();
                  System.out.println(name);
            }
      Copy after login
    • 导入jar包 dom4j.jar

    • 创建解析器

    • 解析xml 获得document对象

    • SAXReader reader = new SAXReader()

    • Document document = reader.read(url)

    • JAXP sun公司提供的解析 支持dom和sax

    • JDOM

    • DOM4J dom for java民间方式,但是是事实方式,非常好,支持dom

    • 解析xml

    • XPATH 专门用于查询

The above is the detailed content of Take you to a deeper understanding of XML. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Convert XML data to CSV format in Python Convert XML data to CSV format in Python Aug 11, 2023 pm 07:41 PM

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

Handling errors and exceptions in XML using Python Handling errors and exceptions in XML using Python Aug 08, 2023 pm 12:25 PM

Handling Errors and Exceptions in XML Using Python XML is a commonly used data format used to store and represent structured data. When we use Python to process XML, sometimes we may encounter some errors and exceptions. In this article, I will introduce how to use Python to handle errors and exceptions in XML, and provide some sample code for reference. Use try-except statement to catch XML parsing errors When we use Python to parse XML, sometimes we may encounter some

Python parsing special characters and escape sequences in XML Python parsing special characters and escape sequences in XML Aug 08, 2023 pm 12:46 PM

Python parses special characters and escape sequences in XML XML (eXtensibleMarkupLanguage) is a commonly used data exchange format used to transfer and store data between different systems. When processing XML files, you often encounter situations that contain special characters and escape sequences, which may cause parsing errors or misinterpretation of the data. Therefore, when parsing XML files using Python, we need to understand how to handle these special characters and escape sequences. 1. Special characters and

How to handle XML and JSON data formats in C# development How to handle XML and JSON data formats in C# development Oct 09, 2023 pm 06:15 PM

How to handle XML and JSON data formats in C# development requires specific code examples. In modern software development, XML and JSON are two widely used data formats. XML (Extensible Markup Language) is a markup language used to store and transmit data, while JSON (JavaScript Object Notation) is a lightweight data exchange format. In C# development, we often need to process and operate XML and JSON data. This article will focus on how to use C# to process these two data formats, and attach

At a glance: A quick overview of how to open JSP files At a glance: A quick overview of how to open JSP files Jan 31, 2024 pm 09:28 PM

JSP file opening method JSP (JavaServerPages) is a dynamic web page technology that allows programmers to embed Java code in HTML pages. JSP files are text files that contain HTML code, XML tags, and Java code. When a JSP file is requested, it is compiled into a JavaServlet and then executed by the web server. Methods of Opening JSP Files There are several ways to open JSP files. The easiest way is to use a text editor,

Using Python to implement data verification in XML Using Python to implement data verification in XML Aug 10, 2023 pm 01:37 PM

Using Python to implement data validation in XML Introduction: In real life, we often deal with a variety of data, among which XML (Extensible Markup Language) is a commonly used data format. XML has good readability and scalability, and is widely used in various fields, such as data exchange, configuration files, etc. When processing XML data, we often need to verify the data to ensure the integrity and correctness of the data. This article will introduce how to use Python to implement data verification in XML and give the corresponding

Differences between Go language and Golang: Do you know it? Differences between Go language and Golang: Do you know it? Feb 24, 2024 pm 06:06 PM

Go and Golang are the same programming language and there is no substantial difference between them. Go is the official name of the programming language, and Golang is the abbreviation commonly used by Go language developers in the Internet field. In this article, we will explore the characteristics, uses, and some specific code examples of the Go language to help readers better understand this powerful programming language. Go language is a statically compiled programming language developed by Google. It has the characteristics of efficiency, simplicity, and strong concurrency, and is designed to improve programmers' work efficiency.

See all articles