PHP使用xmllint命令處理xml與html的方法
本文實(shí)例講述了PHP使用xmllint命令處理xml與html的方法。分享給大家供大家參考。具體分析如下:
xmllint是一個(gè)很方便的處理及驗(yàn)證xml、處理html的工具,linux下只要安裝libxml2就可以使用這個(gè)命令。首先看下其結(jié)合--html 、--xpath參數(shù)處理html時(shí)的例子:
示例如下:
上例中主要是通過(guò)在123cha上查詢的IP地址的歸屬情況后,通過(guò)提取結(jié)果(ul#csstb),只獲取文本部分的內(nèi)容。上面的腳本語(yǔ)句執(zhí)行后的結(jié)果如下:
[您的查詢]:8.8.8.8
本站主數(shù)據(jù):
美國(guó)
本站輔數(shù)據(jù):Google Public DNS提供:hypo
美國(guó) Google免費(fèi)的Google Public DNS提供:zwstar參考數(shù)據(jù)一:美國(guó)
參考數(shù)據(jù)二:美國(guó)
下面再結(jié)合示例看下其他主要參數(shù)的用法。
1、 --format
此參數(shù)用于格式化xml,使其具有良好的可讀性。
假設(shè)有xml(person.xml)內(nèi)容如下:
執(zhí)行如下操作后其輸出為更易讀的xml格式:
<?xml version="1.0"?>
<person>
<name>ball</name>
<age>30</age>
<sex>male</sex>
</person>
2、 --noblanks
與--format相反,有時(shí)為了節(jié)省傳輸量,我們希望去掉xml中的空白,這時(shí)我們可以使用--noblanks命令。
假設(shè)xml(person.xml)內(nèi)容如下
<person>
<name>ball</name>
<age>30</age>
<sex>male</sex>
</person>
執(zhí)行該參數(shù)操作后,其輸出結(jié)果為:
<?xml version="1.0"?>
<person><name>ball</name><age>30</age><sex>male</sex></person>
3、--schema
使用scheam驗(yàn)證xml文件的正確性(XML Schema 是基于 XML 的 DTD 替代者)
假設(shè)有xml文件(person.xml)和scheam文件(person.xsd)文件,內(nèi)容分別如下
person.xml
<person>
<name>ball</name>
<age>30</age>
<sex>male</sex>
</person>
person.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="sex">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="male"/>
<xs:enumeration value="female"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="person">
<xs:complexType>
<xs:all>
<xs:element ref="name"/>
<xs:element ref="age"/>
<xs:element ref="sex"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
按如下命令執(zhí)行后的結(jié)果是:
<?xml version="1.0"?>
<person>
<name>ball</name>
<age>30</age>
<sex>male</sex>
</person>
person.xml validates
注:默認(rèn)情況下,驗(yàn)證后會(huì)輸出驗(yàn)證的文件內(nèi)容,可以使用 --noout選項(xiàng)去掉此輸出,這樣我們可以只得到最后的驗(yàn)證結(jié)果。
person.xml validates
下面我們改動(dòng)person.xml,使這份文件age字段和sex都是不符合xsd定義的。
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
可以看到xmllint成功的報(bào)出了錯(cuò)誤!
4、 關(guān)于--schema的輸出
在講輸出之前先看下面一個(gè)場(chǎng)景,假如你想通過(guò)php執(zhí)行xmllint然后拿到返回結(jié)果,你的代碼通常應(yīng)該是這個(gè)樣子valid.php
$command = "xmllint --noout --schema person.xsd person.xml";
exec($command, $output, $retval);
//出錯(cuò)時(shí)返回值不為0
if ($retval != 0){
var_dump($output);
}
else{
echo "yeah!";
}
我們保持上文中person.xml的錯(cuò)誤。
執(zhí)行此代碼,你會(huì)發(fā)現(xiàn),你拿到的output不是錯(cuò)誤,而是array(0) {}, amazing!
為什么會(huì)這樣呢?
因?yàn)閤mllint --schema,如果驗(yàn)證出錯(cuò)誤,錯(cuò)誤信息并不是通過(guò)標(biāo)準(zhǔn)輸出(stdout)顯示的,而是通過(guò)標(biāo)準(zhǔn)錯(cuò)誤(stderr)進(jìn)行顯示的。
而exec的output參數(shù)拿到的,只能是標(biāo)準(zhǔn)輸出(stdout)顯示的內(nèi)容。
所以,為了拿到出錯(cuò)信息,我們需要將標(biāo)準(zhǔn)錯(cuò)誤重定向到標(biāo)準(zhǔn)輸出,對(duì)應(yīng)修改代碼:
再次執(zhí)行valid.php,錯(cuò)誤信息順利拿到!
例子如下:
首先建立一份 xml 文檔,命名為 po.xml,其內(nèi)容如下:
<purchaseOrder orderDate="1999-10-20">
<shipTo country="US">
<name>Alice Smith</name>
<street>123 Maple Street</street>
<city>Mill Valley</city>
<state>CA</state>
<zip>90952</zip>
</shipTo>
<billTo country="US">
<name>Robert Smith</name>
<street>8 Oak Avenue</street>
<city>Old Town</city>
<state>PA</state>
<zip>95819</zip>
</billTo>
<comment>Hurry, my lawn is going wild!</comment>
<items>
<item partNum="872-AA">
<productName>Lawnmower</productName>
<quantity>1</quantity>
<USPrice>148.95</USPrice>
<comment>Confirm this is electric</comment>
</item>
<item partNum="926-AA">
<productName>Baby Monitor</productName>
<quantity>1</quantity>
<USPrice>39.98</USPrice>
<shipDate>1999-05-21</shipDate>
</item>
</items>
</purchaseOrder>
然后為 po.xml 寫的 schema 文件,取名為 po.xsd,內(nèi)容如下:
<xsd:annotation>
<xsd:documentation xml:lang="en">
Purchase order schema for Example.com.
Copyright 2000 Example.com. All rights reserved.
</xsd:documentation>
</xsd:annotation>
<xsd:element name="purchaseOrder" type="PurchaseOrderType"/>
<xsd:element name="comment" type="xsd:string"/>
<xsd:complexType name="PurchaseOrderType">
<xsd:sequence>
<xsd:element name="shipTo" type="USAddress"/>
<xsd:element name="billTo" type="USAddress"/>
<xsd:element ref="comment" minOccurs="0"/>
<xsd:element name="items" type="Items"/>
</xsd:sequence>
<xsd:attribute name="orderDate" type="xsd:date"/>
</xsd:complexType>
<xsd:complexType name="USAddress">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="street" type="xsd:string"/>
<xsd:element name="city" type="xsd:string"/>
<xsd:element name="state" type="xsd:string"/>
<xsd:element name="zip" type="xsd:decimal"/>
</xsd:sequence>
<xsd:attribute name="country" type="xsd:NMTOKEN" fixed="US"/>www.dbjr.com.cn
</xsd:complexType>
<xsd:complexType name="Items">
<xsd:sequence>
<xsd:element name="item" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="productName" type="xsd:string"/>
<xsd:element name="quantity">
<xsd:simpleType>
<xsd:restriction base="xsd:positiveInteger">
<xsd:maxExclusive value="100"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="USPrice" type="xsd:decimal"/>
<xsd:element ref="comment" minOccurs="0"/>
<xsd:element name="shipDate" type="xsd:date" minOccurs="0"/>
</xsd:sequence>
<xsd:attribute name="partNum" type="SKU" use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<!-- Stock Keeping Unit, a code for identifying products -->
<xsd:simpleType name="SKU">
<xsd:restriction base="xsd:string">
<xsd:pattern value="d{3}-[A-Z]{2}"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
使用 xmllint 對(duì) po.xml 文件進(jìn)行校驗(yàn):
希望本文所述對(duì)大家的PHP程序設(shè)計(jì)有所幫助。
- php與XML、XSLT、Mysql的結(jié)合運(yùn)用實(shí)現(xiàn)代碼
- PHP XML備份Mysql數(shù)據(jù)庫(kù)
- 一個(gè)用于MySQL的PHP XML類
- 用PHP實(shí)現(xiàn)XML備份Mysql數(shù)據(jù)庫(kù)
- php中Array2xml類實(shí)現(xiàn)數(shù)組轉(zhuǎn)化成XML實(shí)例
- PHP中SimpleXML函數(shù)用法分析
- PHP 讀取和編寫 XML
- php實(shí)現(xiàn)mysql事務(wù)處理的方法
- php將csv文件導(dǎo)入到mysql數(shù)據(jù)庫(kù)的方法
- php中實(shí)現(xiàn)xml與mysql數(shù)據(jù)相互轉(zhuǎn)換的方法
相關(guān)文章
PHP接入Apple對(duì)access_token/identityToken進(jìn)行JWT驗(yàn)證流程詳解
JWT(JSON Web Token)是為了在網(wǎng)絡(luò)應(yīng)用環(huán)境間傳遞聲明而執(zhí)行的一種基于JSON的開放標(biāo)準(zhǔn)。本文將為大家介紹PHP如何實(shí)現(xiàn)JWT登錄鑒權(quán),需要的可以參考一下2022-09-09關(guān)于PHP5和PHP7中數(shù)組實(shí)現(xiàn)方式的比較總結(jié)
PHP7比PHP5性能提升了兩倍,全面一致的64位支持,以前的許多致命錯(cuò)誤,現(xiàn)在改成拋出異常,下面這篇文章主要給大家介紹了關(guān)于PHP5和PHP7中數(shù)組實(shí)現(xiàn)方式比較的相關(guān)資料,需要的朋友可以參考下2021-09-09PHP調(diào)用MsSQL Server 2012存儲(chǔ)過(guò)程獲取多結(jié)果集(包含output參數(shù))的詳解
本篇文章是對(duì)PHP調(diào)用MsSQL Server 2012存儲(chǔ)過(guò)程獲取多結(jié)果集(包含output參數(shù))的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07php基于curl重寫file_get_contents函數(shù)實(shí)例
這篇文章主要介紹了php基于curl重寫file_get_contents函數(shù)的方法,結(jié)合實(shí)例形式分析了php使用curl重寫file_get_contents函數(shù)實(shí)現(xiàn)屏蔽錯(cuò)誤提示的相關(guān)技巧,需要的朋友可以參考下2016-11-11在數(shù)據(jù)量大(超過(guò)10萬(wàn))的情況下
在數(shù)據(jù)量大(超過(guò)10萬(wàn))的情況下...2007-01-01php中關(guān)于token驗(yàn)證的相關(guān)問題詳解
這篇文章主要介紹了php中關(guān)于token驗(yàn)證的相關(guān)問題詳解的相關(guān)資料,需要的朋友可以參考下2023-05-05[PHP]經(jīng)常用到的實(shí)用函數(shù)集合
[PHP]經(jīng)常用到的實(shí)用函數(shù)集合...2007-11-11PHP生成網(wǎng)頁(yè)快照 不用COM不用擴(kuò)展.
PHP生成網(wǎng)頁(yè)快照, 不用COM不用擴(kuò)展.2010-02-02