PHP使用xmllint命令处理xml与html的方法
本文实例讲述了PHP使用xmllint命令处理xml与html的方法。分享给大家供大家参考。具体分析如下:
xmllint是一个很方便的处理及验证xml、处理html的工具,linux下只要安装libxml2就可以使用这个命令。首先看下其结合--html、--xpath参数处理html时的例子:
示例如下:
curlhttps://www.nhooo.com/ip/?q=8.8.8.82>/dev/null|xmllint--html--xpath"//ul[@id='csstb']"-2>/dev/null|sed-e's/<[^>]*>//g'
上例中主要是通过在123cha上查询的IP地址的归属情况后,通过提取结果(ul#csstb),只获取文本部分的内容。上面的脚本语句执行后的结果如下:
[您的查询]:8.8.8.8
本站主数据:
美国
本站辅数据:GooglePublicDNS提供:hypo
美国Google免费的GooglePublicDNS提供:zwstar参考数据一:美国
参考数据二:美国
下面再结合示例看下其他主要参数的用法。
1、--format
此参数用于格式化xml,使其具有良好的可读性。
假设有xml(person.xml)内容如下:
<person><name>ball</name><age>30</age<sex>male</sex></person>
执行如下操作后其输出为更易读的xml格式:
#xmllint--formatperson.xml
<?xmlversion="1.0"?>
<person>
<name>ball</name>
<age>30</age>
<sex>male</sex>
</person>2、--noblanks
与--format相反,有时为了节省传输量,我们希望去掉xml中的空白,这时我们可以使用--noblanks命令。
假设xml(person.xml)内容如下
<?xmlversion="1.0"?>
<person>
<name>ball</name>
<age>30</age>
<sex>male</sex>
</person>执行该参数操作后,其输出结果为:
#xmllint--noblanksperson.xml <?xmlversion="1.0"?> <person><name>ball</name><age>30</age><sex>male</sex></person>
3、--schema
使用scheam验证xml文件的正确性(XMLSchema是基于XML的DTD替代者)
假设有xml文件(person.xml)和scheam文件(person.xsd)文件,内容分别如下
person.xml
<?xmlversion="1.0"?>
<person>
<name>ball</name>
<age>30</age>
<sex>male</sex>
</person>person.xsd
<?xmlversion="1.0"?>
<xs:schemaxmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:elementname="name"type="xs:string"/>
<xs:elementname="age"type="xs:integer"/>
<xs:elementname="sex">
<xs:simpleType>
<xs:restrictionbase="xs:string">
<xs:enumerationvalue="male"/>
<xs:enumerationvalue="female"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:elementname="person">
<xs:complexType>
<xs:all>
<xs:elementref="name"/>
<xs:elementref="age"/>
<xs:elementref="sex"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>按如下命令执行后的结果是:
#xmllint--schemaperson.xsdperson.xml
<?xmlversion="1.0"?>
<person>
<name>ball</name>
<age>30</age>
<sex>male</sex>
</person>
person.xmlvalidates
注:默认情况下,验证后会输出验证的文件内容,可以使用--noout选项去掉此输出,这样我们可以只得到最后的验证结果。
#xmllint--noout--schemaperson.xsdperson.xml
person.xmlvalidates
下面我们改动person.xml,使这份文件age字段和sex都是不符合xsd定义的。
#xmllint--noout--schemaperson.xsdperson.xml
person.xml:4:elementage:Schemasvalidityerror:Element'age':'notage'isnotavalidvalueoftheatomictype'xs:integer'.
person.xml:5:elementsex:Schemasvalidityerror:Element'sex':[facet'enumeration']Thevalue'test'isnotanelementoftheset{'male','female'}.
person.xml:5:elementsex:Schemasvalidityerror:Element'sex':'test'isnotavalidvalueofthelocalatomictype.
person.xmlfailstovalidate可以看到xmllint成功的报出了错误!
4、关于--schema的输出
在讲输出之前先看下面一个场景,假如你想通过php执行xmllint然后拿到返回结果,你的代码通常应该是这个样子valid.php
<?php
$command="xmllint--noout--schemaperson.xsdperson.xml";
exec($command,$output,$retval);
//出错时返回值不为0
if($retval!=0){
var_dump($output);
}
else{
echo"yeah!";
}我们保持上文中person.xml的错误。
执行此代码,你会发现,你拿到的output不是错误,而是array(0){},amazing!
为什么会这样呢?
因为xmllint--schema,如果验证出错误,错误信息并不是通过标准输出(stdout)显示的,而是通过标准错误(stderr)进行显示的。
而exec的output参数拿到的,只能是标准输出(stdout)显示的内容。
所以,为了拿到出错信息,我们需要将标准错误重定向到标准输出,对应修改代码:
$command="xmllint--noout--schemaperson.xsdperson.xml2>$1";
再次执行valid.php,错误信息顺利拿到!
例子如下:
首先建立一份xml文档,命名为po.xml,其内容如下:
<?xmlversion="1.0"?>
<purchaseOrderorderDate="1999-10-20">
<shipTocountry="US">
<name>AliceSmith</name>
<street>123MapleStreet</street>
<city>MillValley</city>
<state>CA</state>
<zip>90952</zip>
</shipTo>
<billTocountry="US">
<name>RobertSmith</name>
<street>8OakAvenue</street>
<city>OldTown</city>
<state>PA</state>
<zip>95819</zip>
</billTo>
<comment>Hurry,mylawnisgoingwild!</comment>
<items>
<itempartNum="872-AA">
<productName>Lawnmower</productName>
<quantity>1</quantity>
<USPrice>148.95</USPrice>
<comment>Confirmthisiselectric</comment>
</item>
<itempartNum="926-AA">
<productName>BabyMonitor</productName>
<quantity>1</quantity>
<USPrice>39.98</USPrice>
<shipDate>1999-05-21</shipDate>
</item>
</items>
</purchaseOrder>然后为po.xml写的schema文件,取名为po.xsd,内容如下:
<xsd:schemaxmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:annotation>
<xsd:documentationxml:lang="en">
PurchaseorderschemaforExample.com.
Copyright2000Example.com.Allrightsreserved.
</xsd:documentation>
</xsd:annotation>
<xsd:elementname="purchaseOrder"type="PurchaseOrderType"/>
<xsd:elementname="comment"type="xsd:string"/>
<xsd:complexTypename="PurchaseOrderType">
<xsd:sequence>
<xsd:elementname="shipTo"type="USAddress"/>
<xsd:elementname="billTo"type="USAddress"/>
<xsd:elementref="comment"minOccurs="0"/>
<xsd:elementname="items" type="Items"/>
</xsd:sequence>
<xsd:attributename="orderDate"type="xsd:date"/>
</xsd:complexType>
<xsd:complexTypename="USAddress">
<xsd:sequence>
<xsd:elementname="name" type="xsd:string"/>
<xsd:elementname="street"type="xsd:string"/>
<xsd:elementname="city" type="xsd:string"/>
<xsd:elementname="state" type="xsd:string"/>
<xsd:elementname="zip" type="xsd:decimal"/>
</xsd:sequence>
<xsd:attributename="country"type="xsd:NMTOKEN"fixed="US"/>www.nhooo.com
</xsd:complexType>
<xsd:complexTypename="Items">
<xsd:sequence>
<xsd:elementname="item"minOccurs="0"maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:elementname="productName"type="xsd:string"/>
<xsd:elementname="quantity">
<xsd:simpleType>
<xsd:restrictionbase="xsd:positiveInteger">
<xsd:maxExclusivevalue="100"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:elementname="USPrice" type="xsd:decimal"/>
<xsd:elementref="comment" minOccurs="0"/>
<xsd:elementname="shipDate"type="xsd:date"minOccurs="0"/>
</xsd:sequence>
<xsd:attributename="partNum"type="SKU"use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<!--StockKeepingUnit,acodeforidentifyingproducts-->
<xsd:simpleTypename="SKU">
<xsd:restrictionbase="xsd:string">
<xsd:patternvalue="d{3}-[A-Z]{2}"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>使用xmllint对po.xml文件进行校验:
$xmllint -schemapo.xsdpo.xml如果无出错信息,就说明校验通过了。
希望本文所述对大家的PHP程序设计有所帮助。