欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP封裝的XML簡單操作類完整實(shí)例

 更新時間:2017年11月13日 14:46:33   作者:影天  
這篇文章主要介紹了PHP封裝的XML簡單操作類,結(jié)合完整實(shí)例形式分析了php針對xml文件進(jìn)行載入、讀取及寫入相關(guān)操作技巧的封裝與使用方法,需要的朋友可以參考下

本文實(shí)例講述了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é)點(diǎn)的屬性列表(只讀)
childNodes   存儲節(jié)點(diǎn)的子節(jié)點(diǎn)列表(只讀)
dataType   返回此節(jié)點(diǎn)的數(shù)據(jù)類型
Definition   以DTD或XML模式給出的節(jié)點(diǎn)的定義(只讀)
Doctype   指定文檔類型節(jié)點(diǎn)(只讀)
documentElement   返回文檔的根元素(可讀寫)
firstChild   返回當(dāng)前節(jié)點(diǎn)的第一個子節(jié)點(diǎn)(只讀)
Implementation   返回XMLDOMImplementation對象
lastChild   返回當(dāng)前節(jié)點(diǎn)最后一個子節(jié)點(diǎn)(只讀)
nextSibling   返回當(dāng)前節(jié)點(diǎn)的下一個兄弟節(jié)點(diǎn)(只讀)
nodeName   返回節(jié)點(diǎn)的名字(只讀)
nodeType   返回節(jié)點(diǎn)的類型(只讀)
nodeTypedValue   存儲節(jié)點(diǎn)值(可讀寫)
nodeValue   返回節(jié)點(diǎn)的文本(可讀寫)
ownerDocument   返回包含此節(jié)點(diǎn)的根文檔(只讀)
parentNode   返回父節(jié)點(diǎn)(只讀)
Parsed   返回此節(jié)點(diǎn)及其子節(jié)點(diǎn)是否已經(jīng)被解析(只讀)
Prefix   返回名稱空間前綴(只讀)
preserveWhiteSpace   指定是否保留空白(可讀寫)
previousSibling   返回此節(jié)點(diǎn)的前一個兄弟節(jié)點(diǎn)(只讀)
Text   返回此節(jié)點(diǎn)及其后代的文本內(nèi)容(可讀寫)
url   返回最近載入的XML文檔的URL(只讀)
Xml   返回節(jié)點(diǎn)及其后代的XML表示(只讀)
方法:
appendChild   為當(dāng)前節(jié)點(diǎn)添加一個新的子節(jié)點(diǎn),放在最后的子節(jié)點(diǎn)后
cloneNode   返回當(dāng)前節(jié)點(diǎn)的拷貝
createAttribute   創(chuàng)建新的屬性
createCDATASection   創(chuàng)建包括給定數(shù)據(jù)的CDATA段
createComment   創(chuàng)建一個注釋節(jié)點(diǎn)
createDocumentFragment   創(chuàng)建DocumentFragment對象
createElement   創(chuàng)建一個元素節(jié)點(diǎn)
createEntityReference   創(chuàng)建EntityReference對象
createNode   創(chuàng)建給定類型,名字和命名空間的節(jié)點(diǎn)
createPorcessingInstruction   創(chuàng)建操作指令節(jié)點(diǎn)
createTextNode   創(chuàng)建包括給定數(shù)據(jù)的文本節(jié)點(diǎn)
getElementsByTagName   返回指定名字的元素集合
hasChildNodes   返回當(dāng)前節(jié)點(diǎn)是否有子節(jié)點(diǎn)
insertBefore   在指定節(jié)點(diǎn)前插入子節(jié)點(diǎn)
Load   導(dǎo)入指定位置的XML文檔
loadXML   導(dǎo)入指定字符串的XML文檔
removeChild   從子結(jié)點(diǎn)列表中刪除指定的子節(jié)點(diǎn)
replaceChild   從子節(jié)點(diǎn)列表中替換指定的子節(jié)點(diǎn)
Save   把XML文件存到指定節(jié)點(diǎn)
selectNodes   對節(jié)點(diǎn)進(jìn)行指定的匹配,并返回匹配節(jié)點(diǎn)列表
selectSingleNode   對節(jié)點(diǎn)進(jìn)行指定的匹配,并返回第一個匹配節(jié)點(diǎn)
transformNode   使用指定的樣式表對節(jié)點(diǎn)及其后代進(jìn)行轉(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文件保留縮進(jìn)樣式
      $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;
  }
  /**
   * 把a(bǔ)rray數(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;
    }
  }
}

簡單用法示例如下:

smp.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<root>
   <posts>
     <id>1</id>
     <title>標(biāo)題一</title>
     <content>詳細(xì)內(nèi)容一</content>
   </posts>
   <posts>
     <id>2</id>
     <title>標(biāo)題二</title>
     <content>詳細(xì)內(nèi)容二</content>
   </posts>
   <posts>
     <id>3</id>
     <title>標(biāo)題三</title>
     <content>詳細(xì)內(nèi)容三</content>
   </posts>
</root>

index.php文件:

include("xml_dom.php");
$xml=new xml_dom("smp.xml");//載入xml文件
$xmlarr=$xml->getData();//讀取xml文件內(nèi)容
var_dump($xmlarr);

運(yùn)行結(jié)果:

array(1) {
 ["posts"]=>
 array(3) {
  [0]=>
  array(3) {
   ["id"]=>
   string(1) "1"
   ["title"]=>
   string(9) "標(biāo)題一"
   ["content"]=>
   string(15) "詳細(xì)內(nèi)容一"
  }
  [1]=>
  array(3) {
   ["id"]=>
   string(1) "2"
   ["title"]=>
   string(9) "標(biāo)題二"
   ["content"]=>
   string(15) "詳細(xì)內(nèi)容二"
  }
  [2]=>
  array(3) {
   ["id"]=>
   string(1) "3"
   ["title"]=>
   string(9) "標(biāo)題三"
   ["content"]=>
   string(15) "詳細(xì)內(nèi)容三"
  }
 }
}

PS:這里再為大家提供幾款關(guān)于xml操作的在線工具供大家參考使用:

在線XML/JSON互相轉(zhuǎn)換工具:
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

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP針對XML文件操作技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP錯誤與異常處理方法總結(jié)》、《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論