本文實例講述了PHP封裝的XML簡單操作類。分享給大家供大家參考,具體如下:
xml_dom.php封裝類文件:
?php
/**
* Class xml_dom
*
nodeType:
1 XML_ELEMENT_NODE(元素類型)
2 XML_ATTRIBUTE_NODE
3 XML_TEXT_NODE
4 XML_CDATA_SECTION_NODE
5 XML_ENTITY_REFERENCE_NODE
6 XML_ENTITY_NODE
7 XML_PROCESSING_INSTRUCTION_NODE
8 XML_COMMENT_NODE(注釋類型)
9 XML_DOCUMENT_NODE
10 XML_DOCUMENT_TYPE_NODE
11 XML_DOCUMENT_FRAGMENT_NODE
12 XML_NOTATION_NODE
*
PHP DOMDocument操作:
屬性:
Attributes 存儲節點的屬性列表(只讀)
childNodes 存儲節點的子節點列表(只讀)
dataType 返回此節點的數據類型
Definition 以DTD或XML模式給出的節點的定義(只讀)
Doctype 指定文檔類型節點(只讀)
documentElement 返回文檔的根元素(可讀寫)
firstChild 返回當前節點的第一個子節點(只讀)
Implementation 返回XMLDOMImplementation對象
lastChild 返回當前節點最后一個子節點(只讀)
nextSibling 返回當前節點的下一個兄弟節點(只讀)
nodeName 返回節點的名字(只讀)
nodeType 返回節點的類型(只讀)
nodeTypedValue 存儲節點值(可讀寫)
nodeValue 返回節點的文本(可讀寫)
ownerDocument 返回包含此節點的根文檔(只讀)
parentNode 返回父節點(只讀)
Parsed 返回此節點及其子節點是否已經被解析(只讀)
Prefix 返回名稱空間前綴(只讀)
preserveWhiteSpace 指定是否保留空白(可讀寫)
previousSibling 返回此節點的前一個兄弟節點(只讀)
Text 返回此節點及其后代的文本內容(可讀寫)
url 返回最近載入的XML文檔的URL(只讀)
Xml 返回節點及其后代的XML表示(只讀)
方法:
appendChild 為當前節點添加一個新的子節點,放在最后的子節點后
cloneNode 返回當前節點的拷貝
createAttribute 創建新的屬性
createCDATASection 創建包括給定數據的CDATA段
createComment 創建一個注釋節點
createDocumentFragment 創建DocumentFragment對象
createElement 創建一個元素節點
createEntityReference 創建EntityReference對象
createNode 創建給定類型,名字和命名空間的節點
createPorcessingInstruction 創建操作指令節點
createTextNode 創建包括給定數據的文本節點
getElementsByTagName 返回指定名字的元素集合
hasChildNodes 返回當前節點是否有子節點
insertBefore 在指定節點前插入子節點
Load 導入指定位置的XML文檔
loadXML 導入指定字符串的XML文檔
removeChild 從子結點列表中刪除指定的子節點
replaceChild 從子節點列表中替換指定的子節點
Save 把XML文件存到指定節點
selectNodes 對節點進行指定的匹配,并返回匹配節點列表
selectSingleNode 對節點進行指定的匹配,并返回第一個匹配節點
transformNode 使用指定的樣式表對節點及其后代進行轉換
*
*/
class xml_dom
{
protected $dblink; // xml連接
protected $dbfile; // xml文件路徑
/**
* xml文件 構造類
* @param $db_file xml文件
*/
public function __construct($db_file)
{
$this->dbfile = $db_file;
if(!file_exists($db_file))
{
// die('未找到數據庫文件');
$this->dblink = new DOMDocument('1.0', 'utf-8');
$root = $this->dblink->createElement('root');
$this->dblink->appendChild($root);
$this->dblink->formatOutput = true; // xml文件保留縮進樣式
$this->dblink->save($this->dbfile);
}
else
{
$this->dblink = new DOMDocument();
$this->dblink->formatOutput = true;
$this->dblink->load($this->dbfile);
}
}
/**
* 遍歷所有元素
* ===============================================
* 標準xml文件,一個元素可能有n個屬性,可用自定義鍵[nodevalue]獲取元素值
* ?xml version="1.0" encoding="utf-8"?>
* table name="posts">
* column name="id">1/column>
* column name="title">標題一/column>
* column name="content">詳細內容一/column>
* /table>
* ===============================================
* 簡單xml文件,沒有屬性,鍵值一一對應
* ?xml version="1.0" encoding="utf-8"?>
* root>
* posts>
* id>1/id>
* title>標題一/title>
* content>詳細內容一/content>
* /posts>
* /root>
* @param $node
* @return array
*/
function getData($node=0){
if(!$node)
{
$node = $this->dblink->documentElement;
}
$array = array();
foreach($node->attributes as $attribute)
{
$key = $attribute->nodeName;
$val = $attribute->nodeValue;
$array[$key] = $val;
}
if(count($array)) // 有屬性,則用[nodevalue]鍵代表值
{
$array['nodevalue'] = $node->nodeValue;
}
// 遞歸遍歷所有子元素
$node_child = $node->firstChild;
while($node_child)
{
if(XML_ELEMENT_NODE == $node_child->nodeType)
{
$tagname = $node_child->tagName;
$result = $this->getData($node_child);
if(isset($array[$tagname])) // 發現有重復tagName的子元素存在,所以改用數組存儲重復tagName的所有子元素
{
if(!is_array($array[$tagname][0]))
{
$tmp = $array[$tagname];
$array[$tagname] = array();
$array[$tagname][] = $tmp;
}
$array[$tagname][] = $result;
}
else
{
$array[$tagname] = $result;
}
}
$node_child = $node_child->nextSibling;
}
if(!count($array)) // 沒有子元素沒有屬性=最末子元素,就返回該元素的nodeValue值
{
return $node->nodeValue;
}
return $array;
}
/**
* 把array數據寫到xml文件(覆蓋)
* @param $data
*/
public function setData($data,$node=0)
{
$is_root = false;
if(!$node)
{
$is_root = true;
$node = $this->dblink->documentElement;
// 清除原數據
$remove = array();
$node_child = $node->firstChild;
while($node_child)
{
$remove[] = $node_child;
$node_child = $node_child->nextSibling;
}
foreach($remove as $r)
{
$node->removeChild($r);
}
}
if(is_array($data))
{
foreach($data as $k=>$v)
{
if(is_array($v))
{
foreach($v as $r)
{
$item = $this->dblink->createElement($k);
$result = $this->setData($r,$item);
$node->appendChild($result);
}
}
else
{
$item = $this->dblink->createElement($k);
$value = $this->dblink->createTextNode($v);
$item->appendChild($value);
$node->appendChild($item);
}
}
}
else
{
$item = $this->dblink->createTextNode($data);
$node->appendChild($item);
}
if($is_root)
{
$this->dblink->save($this->dbfile); // 覆蓋寫入
}
else
{
return $node;
}
}
}
簡單用法示例如下:
smp.xml文件:
?xml version="1.0" encoding="utf-8"?>
root>
posts>
id>1/id>
title>標題一/title>
content>詳細內容一/content>
/posts>
posts>
id>2/id>
title>標題二/title>
content>詳細內容二/content>
/posts>
posts>
id>3/id>
title>標題三/title>
content>詳細內容三/content>
/posts>
/root>
index.php文件:
include("xml_dom.php");
$xml=new xml_dom("smp.xml");//載入xml文件
$xmlarr=$xml->getData();//讀取xml文件內容
var_dump($xmlarr);
運行結果:
array(1) {
["posts"]=>
array(3) {
[0]=>
array(3) {
["id"]=>
string(1) "1"
["title"]=>
string(9) "標題一"
["content"]=>
string(15) "詳細內容一"
}
[1]=>
array(3) {
["id"]=>
string(1) "2"
["title"]=>
string(9) "標題二"
["content"]=>
string(15) "詳細內容二"
}
[2]=>
array(3) {
["id"]=>
string(1) "3"
["title"]=>
string(9) "標題三"
["content"]=>
string(15) "詳細內容三"
}
}
}
PS:這里再為大家提供幾款關于xml操作的在線工具供大家參考使用:
在線XML/JSON互相轉換工具:
http://tools.jb51.net/code/xmljson
在線格式化XML/在線壓縮XML:
http://tools.jb51.net/code/xmlformat
XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress
XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat
更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP針對XML文件操作技巧總結》、《PHP數組(Array)操作技巧大全》、《php字符串(string)用法總結》、《PHP錯誤與異常處理方法總結》、《PHP基本語法入門教程》、《php面向對象程序設計入門教程》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
您可能感興趣的文章:- php+Ajax處理xml與json格式數據的方法示例
- PHP以json或xml格式返回請求數據的方法
- php實現xml與json之間的相互轉換功能實例
- PHP生成json和xml類型接口數據格式
- PHP實現返回JSON和XML的類分享
- php json與xml序列化/反序列化
- php 備份數據庫代碼(生成word,excel,json,xml,sql)
- PHP數組生成XML格式數據的封裝類實例
- php封裝json通信接口詳解及實例
- PHP封裝返回Ajax字符串和JSON數組的方法
- PHP封裝XML和JSON格式數據接口操作示例