PHP uses the xmllint command to process xml and html. xmllintxml_PHP tutorial

Source: Internet
Author: User
PHP uses the xmllint command to process xml and html, xmllintxml. PHP uses the xmllint command to process xml and html. xmllintxml this article describes how PHP uses the xmllint command to process xml and html. Share it with you for your reference. The specific analysis is as follows: how to use the xmllint command to process xml and html, xmllintxml

This example describes how PHP uses 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 http://www.bkjia.com/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:

# Xmllint -- noout -- schema person. xsd person. xml
Person. xml: 4: element age: Schemas validity error: Element 'age': 'Not age' is not a valid value of the atomic type 'xs: integer '.
Person. xml: 5: element sex: Schemas validity error: Element 'Sex': [facet 'enumeration'] The value 'test' is not an element of the set {'male ', 'female '}.
Person. xml: 5: element sex: Schemas validity error: Element 'Sex': 'test' is not a valid value of the local atomic type.
Person. xml fails to validate


An error is reported when xmllint is successful!

4. about -- schema output

Let's take a look at the following scenario before talking about the output. if you want to execute xmllint in php and get the returned result, your code is usually like valid. php

The code is as follows:

<? Php
$ Command = "xmllint -- noout -- schema person. xsd person. xml ";
Exec ($ command, $ output, $ retval );
// When an error occurs, the returned value is not 0.
If ($ retval! = 0 ){
Var_dump ($ output );
}
Else {
Echo "yeah! ";
}


We keep the above person. xml errors.

Run this code and you will find that the output you get is not an error, but array (0) {}, amazing!
Why?

Because xmllint -- schema, if an error is verified, the error message is not displayed by stdout, but by stderr.

The output parameter of exec can only be displayed in standard output (stdout.
Therefore, in order to get the error information, we need to redirect the standard error to the standard output and modify the code accordingly:

The code is as follows:

$ Command = "xmllint -- noout -- schema person. xsd person. xml 2> $1 ";


Run valid. php again to get the error message!

Example:
First, create an xml document named po. xml with the following content:

The code is as follows:

<? Xml version = "1.0"?>


Alice Smith
Maple Street 123
Mill Valley
CA
90952


Robert Smith
8. Oak Avenue
Old Town
PA
95819

Hurry, my lawn is going wild!


Lawnmower
1
148.95
Confirm this is electric


Baby Monitor
1
39.98
2017-05-21



The schema file written for po. xml is named po. xsd. the content is as follows:

The code is as follows:




Purchase order schema for Example.com.
Copyright 2000 Example.com. All rights reserved.





















Www.jb51.net






























Use xmllint to verify the po. xml file:

The code is as follows:

$ Xmllint-schema po. xsd po. xml

If no error message is displayed, the verification is successful.

I hope this article will help you with PHP programming.

Examples in this article describes how PHP uses the xmllint command to process xml and html. Share it with you for your reference. The specific analysis 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.