首頁 >php教程 >php手册 >PHP创建与解析 XML 1 (36),xml36

PHP创建与解析 XML 1 (36),xml36

WBOY
WBOY原創
2016-06-13 09:03:231194瀏覽

PHP创建与解析 XML 1 (36),xml36

一.使用SimpleXML操控XML

要处理XML 文件,有两种传统的处理思路:SAX 和DOM。SAX 基于事件触发机制,对XML 文件进行一次扫描,完成要进行的处理;DOM 则将整个XML 文件构造为一棵DOM树,通过对DOM 树的遍历完成处理。这两种方法各有优缺点,SAX 的处理思路相对抽象,DOM 的处理过程相对烦琐,都不很适合新手的入门。PHP5 推出了一套新的XML 处理函数,即SimpleXML。名如其实,SimpleXML 本身小巧精干,只提供了少量的几个方法函数,但用它处理起XML 文件功能却非常强大,操作也非常的简单。

  1.创建XML文件

$_xml =<<<<span>xml
</span><?xml version=<span>"</span><span>1.0</span><span>"</span> encoding=<span>"</span><span>utf-8</span><span>"</span>?>
<root>
<version><span>1.0</span></version>
<info>xml解析测试</info>
<user>
<name>张三</name>
<url>http:<span>//</span><span>www.ss.com</url></span>
<author sex=<span>"</span><span>男</span><span>"</span>>张三</author>
</user>
<user>
<name>宿舍</name>
<url>http:<span>//</span><span>www.ss.com</url></span>
<author sex=<span>"</span><span>女</span><span>"</span>>谁谁谁</author>
</user>
<user>
<name>电驴</name>
<url>http:<span>//</span><span>www.ss.com</url></span>
<author sex=<span>"</span><span>男</span><span>"</span>>姓黄的</author>
</user>
</root><span>
xml;
$_sxe</span>= <span>new</span> SimpleXMLElement($_xml); <span>//</span><span>创建对象解析xml字符串</span>
$_sxe->asXML(<span>'</span><span>test.xml</span><span>'</span>); <span>//</span><span>生成XML文件</span>

  2.载入XML文件

$_sxe= simplexml_load_file(<span>"</span><span>test.xml</span><span>"</span>); <span>//</span><span>载入XML文件</span>
var_dump($_sxe); <span>//</span><span>输出相关信息</span>
print_r($_sxe); <span>//</span><span>输出主要信息</span>
Reflection::export(<span>new</span> ReflectionClass($sxe)); <span>//</span><span>用反射查看详情</span>

  3.解析XML文件

$_sxe= simplexml_load_file(<span>"</span><span>test.xml</span><span>"</span>); <span>//</span><span>载入XML文件</span>
var_dump($_sxe); <span>//</span><span>输出相关信息</span>
print_r($_sxe); <span>//</span><span>输出主要信息</span>
Reflection::export(<span>new</span> ReflectionClass($_sxe)); <span>//</span><span>用发射查看详情</span>
echo $_sxe->asXML();<span>//</span><span>打印整个XML</span>

  4.读出XML数据

$_sxe= simplexml_load_file(<span>"</span><span>test.xml</span><span>"</span><span>);
</span><span>//</span><span>读取一级节点的值,比如version标签</span>
echo $_sxe-><span>version;
</span><span>//</span><span>如果有多个,可以设置它的数字下标</span>
echo $_sxe->version[<span>2</span><span>];
</span><span>//</span><span>如果想要全部打印出来,可以用遍历</span>
<span>foreach</span> ($_sxe->version <span>as</span><span> $_version) {
echo </span><span>'</span><span>[</span><span>'</span>.$_version.<span>'</span><span>]</span><span>'</span><span>;
}
</span><span>//</span><span>访问二级节点的name</span>
echo $_sxe->user[<span>1</span>]-><span>name;
</span><span>//</span><span>获取所有二级节点的name值</span>
<span>foreach</span> ($_sxe->user <span>as</span><span> $_user) {
echo </span><span>'</span><span>[</span><span>'</span>.$_user->name.<span>'</span><span>]</span><span>'</span><span>;
}
</span><span>//</span><span>获取二级节点的标签的属性</span>
echo $_sxe->user[<span>1</span>]->author->attributes();

  5.使用XPath来获取节点

$_sxe= simplexml_load_file(<span>"</span><span>test.xml</span><span>"</span><span>);
</span><span>//</span><span>使用XPath获取节点信息</span>
$_version = $_sxe->xpath(<span>'</span><span>/root/version</span><span>'</span><span>);
echo $_version[</span><span>1</span><span>];
</span><span>//</span><span>遍历version</span>
<span>foreach</span> ($_version <span>as</span><span> $_v) {
echo </span><span>'</span><span>[</span><span>'</span>.$_v.<span>'</span><span>]</span><span>'</span><span>;
}
</span><span>//</span><span>访问二级节点</span>
$_user = $_sxe->xpath(<span>'</span><span>/root/user</span><span>'</span><span>);
echo $_user[</span><span>2</span>]-><span>name;
</span><span>//</span><span>遍历二级节点</span>
<span>foreach</span> ($_user <span>as</span><span> $_u) {
echo </span><span>'</span><span>[</span><span>'</span>.$_u->name.<span>'</span><span>]</span><span>'</span><span>;
}
</span><span>//</span><span>访问属性</span>
echo $_user[<span>1</span>]->author->attributes();

二.使用DOMDocument操控XML

很多情况下,手工生成标记要求从上到下生成文档,必须保证标签都是完整的,开始和结束标签。尽管借助于一些PHP函数或类可以有所改善,不过PHP还提供了一组更有帮助的内置对象和函数。文档对象模型(Document Object Model,DOM)提供了一个树状结构,能很容易创建和处理标签。

  1.DOMDocument解析XML

<span>//</span><span>创建一个DOMDocument()</span>
$_doc = <span>new</span><span> DOMDocument();
</span><span>//</span><span>载入xml</span>
$_doc->load(<span>'</span><span>test.xml</span><span>'</span><span>);
</span><span>//</span><span>取version标签</span>
$_version = $_doc->getElementsByTagName(<span>'</span><span>version</span><span>'</span><span>);
echo $_version</span>->item(<span>2</span>)-><span>nodeValue;
</span><span>//</span><span>遍历version标签</span>
<span>foreach</span> ($_version <span>as</span><span> $v) {
echo $v</span>-><span>nodeValue;
}</span>

  2.DOMDocument生成XML

<span>//</span><span>声明xml</span>
$_doc = <span>new</span> DOMDocument(<span>'</span><span>1.0</span><span>'</span>,<span>'</span><span>utf-8</span><span>'</span><span>);
</span><span>//</span><span>排版格式</span>
$_doc->formatOutput = <span>true</span><span>;
</span><span>//</span><span>创建一个主标签</span>
$_root = $_doc->createElement(<span>'</span><span>root</span><span>'</span><span>);
</span><span>//</span><span>创建一个一级标签version</span>
$_version = $_doc->createElement(<span>'</span><span>version</span><span>'</span><span>);
</span><span>//</span><span>给version标签里赋值</span>
$_versionTextNode = $_doc->createTextNode(<span>'</span><span>1.0</span><span>'</span><span>);
</span><span>//</span><span>将值放入version标签里</span>
$_version-><span>appendChild($_versionTextNode);
</span><span>//</span><span>将一级标签version放入root里</span>
$_root-><span>appendChild($_version);
</span><span>//</span><span>将主标签写入xml</span>
$_doc-><span>appendChild($_root);
</span><span>//</span><span>生成xml</span>
$_doc->save(<span>'</span><span>aaa.xml</span><span>'</span>);

 

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn