Home Backend Development XML/RSS Tutorial XML Tutorial - Learn the details of XML syntax with an example

XML Tutorial - Learn the details of XML syntax with an example

Mar 14, 2017 pm 04:12 PM

The syntax rules of

XML are both very simple and very strict. These rules are easy to learn and easy to use. Because of this, creating software that can read and manipulate XML is not difficult. An example of an XML document XML uses a simple syntax that is self-describing. The syntax rules of XML are both very simple and very strict. These rules are easy to learn and easy to use.
Because of this, it is not difficult to create software that can read and manipulate XML An example of an XML document
XML uses a simple syntax that is self-describing #.
##

<?xml version="1.0" encoding="ISO-8859-1"?>

<note>

<to>Tove</to>

<from>Jani</from>

<heading>Reminder</heading>

<body>Don&#39;t forget me this weekend!</body>

</note>
Copy after login

The first line in this document - the XML declaration - defines the version of XML and the character encoding used in the document. In this case, the XML 1.0 specification is followed. , and uses the ISO-8859-1
character set. The following line describes the root element of the document (as if to say: "This document is a sticky note"):

The next four lines describe the four child elements of the root element (to, from, heading, and body):

<to>Tove</to>

<from>Jani</from>

<heading>Reminder</heading>

<body>Don&#39;t forget me this weekend!</body>
Copy after login

The last line defines the root element End of element:

We can see that this XML document contains a note left by Jani to Tove. Now, you should agree with us that XML has perfect Self-describing properties. All elements must have a closing tag

When using XML, it is illegal to omit the closing tag. In HTML, some elements do not necessarily have closing tags. In HTML the following code is legal:

<p>This is a paragraph

<p>This is another paragraph
Copy after login

In XML, all elements must have a closing tag:

<p>This is a paragraph</p>

<p>This is another paragraph</p>
Copy after login


Note: You may have noticed from the above example that the XML declaration does not have a closing tag. This is not an error. Declarations are not part of XML itself. It is not an XML element and does not require a closing tag. XML tags are case-sensitive
Unlike HTML, XML tags are case-sensitive. In XML, the tag and the tag are different.
So the tag must be opened and closed with the same case:
This is wrong. This is correct. XML must be nested correctly

Incorrect tag nesting has no meaning to XML. In HTML, certain elements can be nested incorrectly within each other, like this:

<b><i>This text is bold and italic</b></i>
Copy after login

In XML, all elements must be correctly nested Nested within each other, like this:
This text is bold and italic XML documents must have a root element

All XML must contain a single tag pair that defines the root element. All other elements must be inside this root element.
All elements can have child elements. Child elements must be properly nested within their parent elements:

<root>

  <child>

    <subchild>.....</subchild>

  </child>

</root> 





XML的属性值须加引号
Copy after login


In XML, it is illegal to omit quotes around an attribute value. Similar to HTML, XML can also have attributes (name/value pairs). In XML, XML attribute values ​​must be quoted. Please study the two XML documents below. The first is wrong, the second is correct:

<?xml version="1.0" encoding="ISO-8859-1"?>

<note date=12/11/2002>

<to>Tove</to>

<from>Jani</from>

</note>
Copy after login
<?xml version="1.0" encoding="ISO-8859-1"?>

<note date="12/11/2002">

<to>Tove</to>

<from>Jani</from>

</note>
Copy after login

In the first document, the date attribute is not quoted. This is correct: date="12/11/2002". This is wrong: date=12/11/2002. In XML, whitespace is preserved.

In XML, spaces will not be truncated. This is different from HTML. In HTML, a sentence like this:

Hello              my name is Tove,
Copy after login

will be displayed like this:

Hello my name is Tove,
Copy after login

This is because HTML will treat multiple consecutive The space characters are trimmed to one. In XML, CR/LF will be converted to LF

In XML, a new line (i.e. line feed) is stored as LF (Line Feed, line feed). Are you familiar with typewriters? A typewriter is a mechanical device used in the last century to create printed documents. :-)
After you type a line of text on the typewriter, you need to manually move the printing carriage to the left margin position and manually feed a line.
In Windows applications, new lines are usually stored as a pair of characters: carriage return (CR) and line feed (LF). This character pair is similar to the action of setting a new line on a typewriter. In Unix applications, new lines are usually stored as LF characters. And Macintosh applications only use CR characters to store new lines. Comments in XML
The syntax for writing comments in XML is similar to the syntax of HTML:

<!-- This is a comment -->





XML没什么特殊之处
Copy after login


XML没什么特殊之处。它只是带有被括在角形括号中的标签的纯文本而已。
可处理纯文本文件的软件也可以处理XML。在一个简单的文本编辑器中,XML标签也可被显示出来,不会被特殊地对待。
在可识别XML的(XML-aware)应用程序中,XML标签会被专门处理。根据不同的应用程序种类,这些标签也许会/也许不会被看到,又或许拥有某种功能意义。                                                

The above is the detailed content of XML Tutorial - Learn the details of XML syntax with an example. 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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

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

Convert POJO to XML using Jackson library in Java? Convert POJO to XML using Jackson library in Java? Sep 18, 2023 pm 02:21 PM

Jackson is a Java-based library that is useful for converting Java objects to JSON and JSON to Java objects. JacksonAPI is faster than other APIs, requires less memory area, and is suitable for large objects. We use the writeValueAsString() method of the XmlMapper class to convert the POJO to XML format, and the corresponding POJO instance needs to be passed as a parameter to this method. Syntax publicStringwriteValueAsString(Objectvalue)throwsJsonProcessingExceptionExampleimp

Parsing XML documents with namespaces using Python Parsing XML documents with namespaces using Python Aug 09, 2023 pm 04:25 PM

Use Python to parse XML documents with namespaces. XML is a commonly used data exchange format that can adapt to various application scenarios. When processing XML documents, sometimes you encounter situations with namespaces. Namespace can prevent the conflict of element names in different XML documents and improve the flexibility and scalability of XML. This article will introduce how to use Python to parse XML documents with namespaces and give corresponding code examples. First, we need to import xml.et

See all articles