本文實例講述了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 存儲節(jié)點的屬性列表(只讀) childNodes 存儲節(jié)點的子節(jié)點列表(只讀) dataType 返回此節(jié)點的數(shù)據(jù)類型 Definition 以DTD或XML模式給出的節(jié)點的定義(只讀) Doctype 指定文檔類型節(jié)點(只讀) documentElement 返回文檔的根元素(可讀寫) firstChild 返回當(dāng)前節(jié)點的第一個子節(jié)點(只讀) Implementation 返回XMLDOMImplementation對象 lastChild 返回當(dāng)前節(jié)點最后一個子節(jié)點(只讀) nextSibling 返回當(dāng)前節(jié)點的下一個兄弟節(jié)點(只讀) nodeName 返回節(jié)點的名字(只讀) nodeType 返回節(jié)點的類型(只讀) nodeTypedValue 存儲節(jié)點值(可讀寫) nodeValue 返回節(jié)點的文本(可讀寫) ownerDocument 返回包含此節(jié)點的根文檔(只讀) parentNode 返回父節(jié)點(只讀) Parsed 返回此節(jié)點及其子節(jié)點是否已經(jīng)被解析(只讀) Prefix 返回名稱空間前綴(只讀) preserveWhiteSpace 指定是否保留空白(可讀寫) previousSibling 返回此節(jié)點的前一個兄弟節(jié)點(只讀) Text 返回此節(jié)點及其后代的文本內(nèi)容(可讀寫) url 返回最近載入的XML文檔的URL(只讀) Xml 返回節(jié)點及其后代的XML表示(只讀) 方法: appendChild 為當(dāng)前節(jié)點添加一個新的子節(jié)點,放在最后的子節(jié)點后 cloneNode 返回當(dāng)前節(jié)點的拷貝 createAttribute 創(chuàng)建新的屬性 createCDATASection 創(chuàng)建包括給定數(shù)據(jù)的CDATA段 createComment 創(chuàng)建一個注釋節(jié)點 createDocumentFragment 創(chuàng)建DocumentFragment對象 createElement 創(chuàng)建一個元素節(jié)點 createEntityReference 創(chuàng)建EntityReference對象 createNode 創(chuàng)建給定類型,名字和命名空間的節(jié)點 createPorcessingInstruction 創(chuàng)建操作指令節(jié)點 createTextNode 創(chuàng)建包括給定數(shù)據(jù)的文本節(jié)點 getElementsByTagName 返回指定名字的元素集合 hasChildNodes 返回當(dāng)前節(jié)點是否有子節(jié)點 insertBefore 在指定節(jié)點前插入子節(jié)點 Load 導(dǎo)入指定位置的XML文檔 loadXML 導(dǎo)入指定字符串的XML文檔 removeChild 從子結(jié)點列表中刪除指定的子節(jié)點 replaceChild 從子節(jié)點列表中替換指定的子節(jié)點 Save 把XML文件存到指定節(jié)點 selectNodes 對節(jié)點進行指定的匹配,并返回匹配節(jié)點列表 selectSingleNode 對節(jié)點進行指定的匹配,并返回第一個匹配節(jié)點 transformNode 使用指定的樣式表對節(jié)點及其后代進行轉(zhuǎn)換 * */ class xml_dom { protected $dblink; // xml連接 protected $dbfile; // xml文件路徑 /** * xml文件 構(gòu)造類 * @param $db_file xml文件 */ public function __construct($db_file) { $this->dbfile = $db_file; if(!file_exists($db_file)) { // die('未找到數(shù)據(jù)庫文件'); $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); } } /** * 遍歷所有元素 * =============================================== * 標(biāo)準(zhǔn)xml文件,一個元素可能有n個屬性,可用自定義鍵[nodevalue]獲取元素值 * <?xml version="1.0" encoding="utf-8"?> * <table name="posts"> * <column name="id">1</column> * <column name="title">標(biāo)題一</column> * <column name="content">詳細(xì)內(nèi)容一</column> * </table> * =============================================== * 簡單xml文件,沒有屬性,鍵值一一對應(yīng) * <?xml version="1.0" encoding="utf-8"?> * <root> * <posts> * <id>1</id> * <title>標(biāo)題一</title> * <content>詳細(xì)內(nèi)容一</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])) // 發(fā)現(xiàn)有重復(fù)tagName的子元素存在,所以改用數(shù)組存儲重復(fù)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數(shù)據(jù)寫到xml文件(覆蓋) * @param $data */ public function setData($data,&$node=0) { $is_root = false; if(!$node) { $is_root = true; $node = $this->dblink->documentElement; // 清除原數(shù)據(jù) $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; } } }
簡單用法示例如下:
【網(wǎng)站聲明】本站除付費源碼經(jīng)過測試外,其他素材未做測試,不保證完整性,網(wǎng)站上部分源碼僅限學(xué)習(xí)交流,請勿用于商業(yè)用途。如損害你的權(quán)益請聯(lián)系客服QQ:2655101040 給予處理,謝謝支持。