• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP使用xmllint命令处理xml与html的方法_php技巧

php 搞代码 3年前 (2022-01-26) 44次浏览 已收录 0个评论

本文实例讲述了PHP使用xmllint命令处理xml与html的方法。分享给大家供大家参考。具体分析如下:

xmllint是一个很方便的处理及验证xml、处理html的工具,linux下只要安装libxml2就可以使用这个命令。首先看下其结合–html 、–xpath参数处理html时的例子:

示例如下:

curl http://www.gaodaima.com /ip/?q=8.8.8.8 2>/dev/null | xmllint --html --xpath "//ul[@id='csstb']" - 2>/dev/null | sed -e 's/]*>//g'

上例中主要是通过在123cha上查询的IP地址的归属情况后,通过提取结果(ul#csstb),只获取文本部分的内容。上面的脚本语句执行后的结果如下:

[您的查询]:8.8.8.8
本站主数据:
美国
本站辅数据:Google Public DNS提供:hypo
美国 Google免费的Google Public DNS提供:zwstar参考数据一:美国
参考数据二:美国

下面再结合示例看下其他主要参数的用法。
1、 –format
此参数用于格式化xml,使其具有良好的可读性。
假设有xml(person.xml)内容如下:

ball30</agemale

执行如下操作后其输出为更易读的xml格式:

#xmllint --format person.xml<br /><br />    <br />      ball<br />      30<br />      male<br />    

2、 –noblanks
与–format相反,有时为了节省传输量,我们希望去掉xml中的空白,这时我们可以使用–noblanks命令。
假设xml(person.xml)内容如下

<br />    <br />      ball<br />      30<br />      male<br />    

执行该参数操作后,其输出结果为:

#xmllint --noblanks person.xml<br /><br />    ball30<em>8本文来源gao.dai.ma.com搞@代*码(网$</em><pre>搞代gaodaima码

male
3、–schema
使用scheam验证xml文件的正确性(XML Schema 是基于 XML 的 DTD 替代者)
假设有xml文件(person.xml)和scheam文件(person.xsd)文件,内容分别如下
person.xml

<br />    <br />      ball<br />      30<br />      male<br />    

person.xsd

<br />    <br />      <br />      <br />      <br />        <br />          <br />            <br />            <br />          <br />        <br />      <br />      <br />        <br />          <br />            <br />            <br />            <br />          <br />        <br />      <br />    

按如下命令执行后的结果是:

#xmllint --schema person.xsd person.xml<br /><br />    <br />      ball<br />      30<br />      male<br />    

person.xml validates
注:默认情况下,验证后会输出验证的文件内容,可以使用 –noout选项去掉此输出,这样我们可以只得到最后的验证结果。

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

person.xml validates
下面我们改动person.xml,使这份文件age字段和sex都是不符合xsd定义的。

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

可以看到xmllint成功的报出了错误!

4、 关于–schema的输出

在讲输出之前先看下面一个场景,假如你想通过php执行xmllint然后拿到返回结果,你的代码通常应该是这个样子valid.php

<?php<br />$command = "xmllint --noout --schema person.xsd person.xml";<br />exec($command, $output, $retval);<br />//出错时返回值不为0<br />if ($retval != 0){<br />    var_dump($output);<br />}<br />else{<br />    echo "yeah!";<br />}

我们保持上文中person.xml的错误。

执行此代码,你会发现,你拿到的output不是错误,而是array(0) {}, amazing!
为什么会这样呢?

因为xmllint –schema,如果验证出错误,错误信息并不是通过标准输出(stdout)显示的,而是通过标准错误(stderr)进行显示的。

而exec的output参数拿到的,只能是标准输出(stdout)显示的内容。
所以,为了拿到出错信息,我们需要将标准错误重定向到标准输出,对应修改代码:

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

再次执行valid.php,错误信息顺利拿到!

例子如下:
首先建立一份 xml 文档,命名为 po.xml,其内容如下:

<br /><br />    <br />        Alice Smith<br />        123 Maple Street<br />        Mill Valley<br />        CA<br />        90952<br />    <br />    <br />        Robert Smith<br />        8 Oak Avenue<br />        Old Town<br />        PA<br />        95819<br />    <br />    Hurry, my lawn is going wild!<br />    <br />        <br />            Lawnmower<br />            1<br />            148.95<br />            Confirm this is electric<br />        <br />        <br />            Baby Monitor<br />            1<br />            39.98<br />            1999-05-21<br />        <br />    <br />

然后为 po.xml 写的 schema 文件,取名为 po.xsd,内容如下:

<br /> <br />  <br />   Purchase order schema for Example.com.<br />   Copyright 2000 Example.com. All rights reserved.<br />  <br /> <br /> <br /> <br /> <br />  <br />   <br />   <br />   <br />   <br />  <br />  <br /> <br /> <br />  <br />   <br />   <br />   <br />   <br />   <br />  <br />  www.gaodaima.com<br /> <br /> <br />  <br />   <br />    <br />     <br />      <br />      <br />       <br />        <br />         <br />        <br />       <br />      <br />      <br />      <br />      <br />     <br />     <br />    <br />   <br />  <br /> <br /> <!---ecms -ecms -ecms  Stock Keeping Unit, a code for identifying products --><br /> <br />  <br />   <br />  <br /> <br />

使用 xmllint 对 po.xml 文件进行校验:

$ xmllint   -schema po.xsd po.xml

如果无出错信息,就说明校验通过了。

希望本文所述对大家的PHP程序设计有所帮助。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP使用xmllint命令处理xml与html的方法_php技巧

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址