首頁  >  文章  >  後端開發  >  PHP建立與解析 XML 1 (36)

PHP建立與解析 XML 1 (36)

WBOY
WBOY原創
2016-08-08 09:23:31896瀏覽

一.使用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>
$_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>

  5.使用XPath來獲取節點

$_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();

二.使用DOMDocument操控XML

很多情況下,手工生成標記要求從上到下生成文檔,必須確保標籤都是完整的,開始和結束標籤。儘管借助一些PHP函數或類別可以有所改善,不過PHP也提供了一組更有幫助的內建物件和函數。文件物件模型(Document Object Model,DOM)提供了一個樹狀結構,可以輕鬆建立和處理標籤。

  1.DOMDocument解析XML

$_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();

  2.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>

以上就介紹了PHP創建與解析 XML 1 (36),包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

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