search
HomeBackend DevelopmentXML/RSS TutorialXML Tutorial - Learn the details of XML syntax with an example

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>

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>

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

In XML, all elements must have a closing tag:

<p>This is a paragraph</p>

<p>This is another paragraph</p>


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>

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的属性值须加引号


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>
<?xml version="1.0" encoding="ISO-8859-1"?>

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

<to>Tove</to>

<from>Jani</from>

</note>

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,

will be displayed like this:

Hello my name is Tove,

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没什么特殊之处


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
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
Inside the RSS Document: Essential XML Tags and AttributesInside the RSS Document: Essential XML Tags and AttributesMay 03, 2025 am 12:12 AM

The core structure of RSS documents includes XML tags and attributes. The specific parsing and generation steps are as follows: 1. Read XML files, process and tags. 2. Extract,,, etc. tag information. 3. Handle custom tags and attributes to ensure version compatibility. 4. Use cache and asynchronous processing to optimize performance to ensure code readability.

JSON, XML, and Data Formats: Comparing RSSJSON, XML, and Data Formats: Comparing RSSMay 02, 2025 am 12:20 AM

The main differences between JSON, XML and RSS are structure and uses: 1. JSON is suitable for simple data exchange, with a simple structure and easy to parse; 2. XML is suitable for complex data structures, with a rigorous structure but complex parsing; 3. RSS is based on XML and is used for content release, standardized but limited use.

Troubleshooting XML/RSS Feeds: Common Pitfalls and Expert SolutionsTroubleshooting XML/RSS Feeds: Common Pitfalls and Expert SolutionsMay 01, 2025 am 12:07 AM

The processing of XML/RSS feeds involves parsing and optimization, and common problems include format errors, encoding issues, and missing elements. Solutions include: 1. Use XML verification tools to check for format errors; 2. Ensure encoding consistency and use the chardet library to detect encoding; 3. Use default values ​​or skip the element when missing elements; 4. Use efficient parsers such as lxml and cache parsing results to optimize performance; 5. Pay attention to data consistency and security to prevent XML injection attacks.

Decoding RSS Documents: Reading and Interpreting FeedsDecoding RSS Documents: Reading and Interpreting FeedsApr 30, 2025 am 12:02 AM

The steps to parse RSS documents include: 1. Read the XML file, 2. Use DOM or SAX to parse XML, 3. Extract headings, links and other information, and 4. Process data. RSS documents are XML-based formats used to publish updated content, structures containing, and elements, suitable for building RSS readers or data processing tools.

RSS and XML: The Cornerstone of Web SyndicationRSS and XML: The Cornerstone of Web SyndicationApr 29, 2025 am 12:22 AM

RSS and XML are the core technologies in network content distribution and data exchange. RSS is used to publish frequently updated content, and XML is used to store and transfer data. Development efficiency and performance can be improved through usage examples and best practices in real projects.

RSS Feeds: Exploring XML's Role and PurposeRSS Feeds: Exploring XML's Role and PurposeApr 28, 2025 am 12:06 AM

XML's role in RSSFeed is to structure data, standardize and provide scalability. 1.XML makes RSSFeed data structured, making it easy to parse and process. 2.XML provides a standardized way to define the format of RSSFeed. 3.XML scalability allows RSSFeed to add new tags and attributes as needed.

Scaling XML/RSS Processing: Performance Optimization TechniquesScaling XML/RSS Processing: Performance Optimization TechniquesApr 27, 2025 am 12:28 AM

When processing XML and RSS data, you can optimize performance through the following steps: 1) Use efficient parsers such as lxml to improve parsing speed; 2) Use SAX parsers to reduce memory usage; 3) Use XPath expressions to improve data extraction efficiency; 4) implement multi-process parallel processing to improve processing speed.

RSS Document Formats: Exploring RSS 2.0 and BeyondRSS Document Formats: Exploring RSS 2.0 and BeyondApr 26, 2025 am 12:22 AM

RSS2.0 is an open standard that allows content publishers to distribute content in a structured way. It contains rich metadata such as titles, links, descriptions, release dates, etc., allowing subscribers to quickly browse and access content. The advantages of RSS2.0 are its simplicity and scalability. For example, it allows custom elements, which means developers can add additional information based on their needs, such as authors, categories, etc.

See all articles

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

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.