PHP uses the xmllint command to process XML and HTML methods, xmllintxml_php tutorial

Source: Internet
Author: User
Tags php programming xpath

PHP uses the Xmllint command to handle XML and HTML methods, Xmllintxml


This example describes how PHP uses the xmllint command to process XML and HTML. Share to everyone for your reference. The specific analysis is as follows:

Xmllint is a handy tool for processing and validating XML and HTML, so you can use this command as soon as you install LIBXML2 in Linux. First, take a look at the example of how HTML is handled with--html 、--XPath parameters:

Examples are as follows:
Copy the Code code as follows: Curl http://www.bkjia.com/ip/?q=8.8.8.8 2>/dev/null | Xmllint--html--xpath "//ul[@id = ' csstb ']"-2>/dev/null | Sed-e ' s/<[^>]*>//g '
In the above example, the main result is to obtain the contents of the text part by extracting the results (UL#CSSTB) after the attribution of the IP address queried on the 123cha. The above script statement executes with the following results:

[Your query]:8.8.8.8
Main data of the site:
United States
This site auxiliary data: Google Public DNS provides: Hypo
Google free Google public DNS provides: Zwstar reference data one: United States
Reference Data two: United States

The following example looks at the usage of the other main parameters.
1,--format
This parameter is used to format the XML so that it is well readable.
Suppose you have the following XML (person.xml) content:
Copy the Code code as follows: Ball 30male
After doing this, its output is in an easier-to-read XML format:

Copy the Code code as follows: #xmllint--format person.xml
<?xml version= "1.0"?>

Ball
30
Male

2,--noblanks
In contrast to--format, sometimes we want to get rid of the whitespace in the XML in order to save the transfer, so we can use the--noblanks command.
Suppose the XML (person.xml) content is as follows
Copy the Code code as follows: <?xml version= "1.0"?>

Ball
30
Male

After performing this parameter operation, its output is:

Copy CodeThe code is as follows: #xmllint--noblanks person.xml
<?xml version= "1.0"?>
Ball male
3 、--Schema
Validating the correctness of XML files using Scheam (XML Schema is an XML-based DTD replacement)
Suppose you have an XML file (person.xml) and a Scheam file (person.xsd) file that reads as follows
Person.xml
Copy CodeThe code is as follows: <?xml version= "1.0"?>

Ball
30
Male

Person.xsd
Copy CodeThe code is as follows: <?xml version= "1.0"?>





















The result after executing as follows is:

The

copy Code code is as follows: #xmllint--schema person.xsd person.xml
<?xml version= "1.0"?

Ball
-
male

Person.xml validates
Note: By default, the validated file content is output after validation, and you can use the--noout option to remove this output so that we can get only the final validation results.
Copy the Code code as follows: #xmllint--noout--schema person.xsd person.xml
Person.xml validates
Let's change the person.xml so that the age field and sex of this file are not in accordance with the XSD definition.
Copy the Code code as follows: #xmllint--noout--schema person.xsd person.xml
Person.xml:4: element age:schemas validity error:element ' age ': ' Isn't age ' is not a valid value of the atomic type ' xs:i Nteger '.
Person.xml:5: element sex:schemas validity error:element ' sex ': [facet ' enumeration '] The value ' test ' is not an elemen T 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
Can see Xmllint success of the report error!

4, about the output of--schema

Look at the following scenario before you tell the output, and if you want to execute xmllint in PHP and get the results back, your code should usually look like this valid.php
Copy the Code code as follows: <?php
$command = "Xmllint--noout--schema person.xsd person.xml";
EXEC ($command, $output, $retval);
The return value is not 0 on error
if ($retval! = 0) {
Var_dump ($output);
}
else{
echo "yeah!";
}
We keep the person.xml error in the above.

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

Because Xmllint--schema, if an error is verified, the error message is not displayed by standard output (stdout), but by a standard error (STDERR).

The output parameters of the exec can only be displayed by the standard output (stdout).
So, in order to get the error message, we need to redirect the standard error to the standard output, corresponding to the modified code:
Copy the code as follows: $command = "Xmllint--noout--schema person.xsd person.xml 2>$1";
Execute valid.php again, error message smoothly get!

Examples are as follows:
First, create an XML document named Po.xml, which reads as follows:
Copy CodeThe code is as follows: <?xml version= "1.0"?>


Alice Smith
123 Maple Street
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
1999-05-21



Then the schema file written for Po.xml, named PO.xsd, reads as follows:
Copy CodeThe code is as follows:


Purchase Order schema for example.com.
Copyright example.com. All rights reserved.





















Www.jb51.net






























To verify the Po.xml file using Xmllint:
Copy CodeThe code is as follows: $ xmllint-schema po.xsd Po.xml If there is no error message, the verification is passed.

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/928228.html www.bkjia.com true http://www.bkjia.com/PHPjc/928228.html techarticle PHP uses the xmllint command to handle XML and HTML methods, Xmllintxml This article describes how PHP uses the xmllint command to process XML and HTML. Share to everyone 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.