xmllint是一个很方便的处理及验证xml的工具,linux下只要安装libxml2就可以使用这个命令,下面整理一些常用功能

1. --format

此参数用于格式化xml,使其具有良好的可读性。
假设有xml(person.xml)内容如下:
<person><name>ball</name><age>30</age><sex>male</sex></person>

执行:

xmllint --format person.xml

得到易读的xml

<?xml version="1.0"?>
<person>
<name>ball</name>
<age>30</age>
<sex>male</sex>
</person>

2. --noblanks

与--format相反,有时为了节省传输量,我们希望去掉xml中的空白,这时我们可以使用--noblanks命令。

假设xml(person.xml)内容如下

<?xml version="1.0"?>
<person>
<name>ball</name>
<age>30</age>
<sex>male</sex>
</person>

执行:

xmllint --noblanks person.xml

得到去掉空白后的xml

<?xml version="1.0"?>
<person><name>ball</name><age>30</age><sex>male</sex></person>

3.--schema

使用scheam验证xml文件的正确性(了解schema的知识请猛击这里
假设有xml文件(person.xml)和scheam文件(person.xsd)文件,内容分别如下
 
person.xml
<?xml version="1.0"?>
<person>
<name>ball</name>
<age>30</age>
<sex>male</sex>
</person>

person.xsd

<?xml version="1.0"?>
<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>

执行:

xmllint --schema person.xsd person.xml

得到

<?xml version="1.0"?>
<person>
<name>ball</name>
<age>30</age>
<sex>male</sex>
</person>
person.xml validates

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

执行

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

得到

person.xml validates

下面我们改动person.xml

<?xml version="1.0"?>
<person>
<name>ball</name>
<age>not age</age>
<sex>test</sex>
</person>
显然,这份文件age字段和sex都是不符合xsd定义的。
执行
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

xmllint成功的报出了错误!

4. 关于--schema的输出

在讲输出之前先看下面一个场景,假如你想通过php执行xmllint然后拿到返回结果,你的代码通常应该是这个样子
valid.php
<?php
$command = "xmllint --noout --schema person.xsd person.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 --schema person.xsd person.xml 2>&1";

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

参考:

https://blog.csdn.net/qmhball/article/details/8955588

https://blog.csdn.net/whackw/article/details/51802530

最新文章

  1. 【BZOJ-2555】SubString 后缀自动机 + LinkCutTree
  2. Linux学习笔记&lt;五&gt;
  3. android avd sdk root
  4. vb小菜一枚-----了解“类型推理”
  5. SpringMVC详细示例
  6. .net学习笔记---xml操作及读写
  7. 关于SQL Server 2008添加用户映射问题 解决办法
  8. fedora终端快捷键
  9. TCP/IP四层模型和OSI七层模型的概念
  10. EF调用存储过程遇到的问题
  11. asp.net(c#)修改时的赋值操作
  12. fmri当前相关软件工具整理
  13. ssi(Server Side Includes)介绍
  14. 学习Android NestedScroll
  15. 解题报告 HDU1789 Doing Homework again
  16. javascript学习笔记01--javascript的基本介绍
  17. 如何通过java反射的方式对java私有方法进行单元测试
  18. An internal error occurred during: &quot;Retrieving archetypes:&quot;. GC overhead limit exceeded
  19. 海量数据挖掘MMDS week1: MapReduce
  20. mongodb副本集高可用架构

热门文章

  1. 16 个 Linux 服务器监控命令
  2. windowbuilde02 表格创建
  3. 使用动态代理实现dao接口
  4. Maven错误-Missing artifact com.sun:tools:jar:1.5.0:system 解决方式
  5. bram和dram差别
  6. 让VMware ESXi虚拟交换机支持VLAN
  7. HDU 4268 Alice and Bob(贪心+Multiset的应用)
  8. DirectX11 学习笔记1 - 第一个程序
  9. h5-注册成功
  10. 【BZOJ 1878】 HH的项链