PHP uses the xmllint command to process xml and html

Source: Internet
Author: User
This article mainly introduces how PHP uses the xmllint command to process xml and html, and describes in detail how to use various commands of the xmllint tool to process XML files and html, is a very practical skill and requires

This article mainly introduces how PHP uses the xmllint command to process xml and html, and describes in detail how to use various commands of the xmllint tool to process XML files and html, is a very practical skill and requires

This example describes how to use the xmllint command to process xml and html. Share it with you for your reference. The specific analysis is as follows:

Xmllint is a convenient tool for processing and verifying xml and html. in linux, you only need to install libxml2. First, let's take a look at the example of processing html with -- html and -- xpath parameters:

Example:

The Code is as follows:

Curl/ip /? Q = 8.8.8.8 2>/dev/null | xmllint -- html -- xpath "// ul [@ id = 'csb']"-2>/dev/null | sed-e' s/<[^>] *> // G'


In the preceding example, the attribution of the IP address queried on 123cha is obtained by extracting the result (ul # csstb) and obtaining only the content of the text part. The execution result of the preceding script statement is as follows:

[Your query]: 8.8.8.8
Main Site Data:
USA
Auxiliary Data on this site: Google Public DNS provides: hypo
Google Public DNS is provided free of charge by Google USA: zwstar reference data 1: USA
Reference data 2: United States

Next, let's take a look at the usage of other main parameters with examples.
1. -- format
This parameter is used to format xml to make it readable.
Assume that the content of xml (person. xml) is as follows:

The Code is as follows:

Ball 30Male


After the following operations are performed, the output is in a more readable xml format:

The Code is as follows:

# Xmllint -- format person. xml
<? Xml version = "1.0"?>

Ball
30
Male


2. -- noblanks
In contrast to -- format, sometimes we want to remove the blank content in xml to save the transmission volume. In this case, we can use the -- noblanks command.
Assume that the content of xml (person. xml) is as follows:

The Code is as follows:

<? Xml version = "1.0"?>

Ball
30
Male


After the parameter operation is executed, the output result is:

The Code is as follows:

# Xmllint -- noblanks person. xml
<? Xml version = "1.0"?>
Ball 30 Male


3. -- schema
Use scheam to verify the correctness of the xml file (XML Schema is an XML-based DTD substitution)
Assume that there are xml files (person. xml) and scheam files (person. xsd). The content is as follows:
Person. xml

The Code is as follows:

<? Xml version = "1.0"?>

Ball
30
Male


Person. xsd

The Code is as follows:

<? Xml version = "1.0"?>





















The result is as follows:

The Code is as follows:

# Xmllint -- schema person. xsd person. xml
<? Xml version = "1.0"?>

Ball
30
Male

Person. xml validates
Note: by default, the content of the file to be verified will be output after verification. You can use the -- noout option to remove this output so that we can only get the final verification result.

The Code is as follows:

# Xmllint -- noout -- schema person. xsd person. xml


Person. xml validates
Next, we modify the person. xml file so that the age field and sex of this file do not comply with the xsd definition.

The Code is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.