好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > PHP解析xml格式數據工具類示例

PHP解析xml格式數據工具類示例

熱門標簽:長沙crm外呼系統業務 南寧高頻外呼回撥系統哪家好 江蘇外呼電銷機器人報價 電話機器人危險嗎 離石地圖標注 專業電話機器人批發商 400電話辦理福州市 深圳外呼系統收費 400電話申請方法收費

本文實例講述了PHP解析xml格式數據工具類。分享給大家供大家參考,具體如下:

class ome_xml {
  /**
  * xml資源
  *
  * @var    resource
  * @see    xml_parser_create()
  */
  public $parser;
  /**
  * 資源編碼
  *
  * @var    string
  */
  public $srcenc;
  /**
  * target encoding
  *
  * @var    string
  */
  public $dstenc;
  /**
  * the original struct
  *
  * @access  private
  * @var    array
  */
  public $_struct = array();
  /**
  * Constructor
  *
  * @access    public
  * @param    mixed    [$srcenc] source encoding
  * @param    mixed    [$dstenc] target encoding
  * @return    void
  * @since
  */
  function SofeeXmlParser($srcenc = null, $dstenc = null) {
    $this->srcenc = $srcenc;
    $this->dstenc = $dstenc;
    // initialize the variable.
    $this->parser = null;
    $this->_struct = array();
  }
  /**
  * Parses the XML file
  *
  * @access    public
  * @param    string    [$file] the XML file name
  * @return    void
  * @since
  */
  function xml2array($file) {
    //$this->SofeeXmlParser('utf-8');
  $data = file_get_contents($file);
    $this->parseString($data);
    return $this->getTree();
  }
  function xml3array($file){
  $data = file_get_contents($file);
  $this->parseString($data);
  return $this->_struct;
  }
  /**
  * Parses a string.
  *
  * @access    public
  * @param    string    data XML data
  * @return    void
  */
  function parseString($data) {
    if ($this->srcenc === null) {
      $this->parser = xml_parser_create();
    } else {
      if($this->parser = xml_parser_create($this->srcenc)) {
        return 'Unable to create XML parser resource with '. $this->srcenc .' encoding.';
      }
    }
    if ($this->dstenc !== null) {
      @xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, $this->dstenc) or die('Invalid target encoding');
    }
    xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);  // lowercase tags
    xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1);    // skip empty tags
    if (!xml_parse_into_struct($this->parser, $data, $this->_struct)) {
      /*printf("XML error: %s at line %d",
          xml_error_string(xml_get_error_code($this->parser)),
          xml_get_current_line_number($this->parser)
      );*/
      $this->free();
      return false;
    }
    $this->_count = count($this->_struct);
    $this->free();
  }
  /**
  * return the data struction
  *
  * @access    public
  * @return    array
  */
  function getTree() {
    $i = 0;
    $tree = array();
    $tree = $this->addNode(
      $tree,
      $this->_struct[$i]['tag'],
      (isset($this->_struct[$i]['value'])) ? $this->_struct[$i]['value'] : '',
      (isset($this->_struct[$i]['attributes'])) ? $this->_struct[$i]['attributes'] : '',
      $this->getChild($i)
    );
    unset($this->_struct);
    return $tree;
  }
  /**
  * recursion the children node data
  *
  * @access    public
  * @param    integer    [$i] the last struct index
  * @return    array
  */
  function getChild($i) {
    // contain node data
    $children = array();
    // loop
    while (++$i  $this->_count) {
      // node tag name
      $tagname = $this->_struct[$i]['tag'];
      $value = isset($this->_struct[$i]['value']) ? $this->_struct[$i]['value'] : '';
      $attributes = isset($this->_struct[$i]['attributes']) ? $this->_struct[$i]['attributes'] : '';
      switch ($this->_struct[$i]['type']) {
        case 'open':
          // node has more children
          $child = $this->getChild($i);
          // append the children data to the current node
          $children = $this->addNode($children, $tagname, $value, $attributes, $child);
          break;
        case 'complete':
          // at end of current branch
          $children = $this->addNode($children, $tagname, $value, $attributes);
          break;
        case 'cdata':
          // node has CDATA after one of it's children
          $children['value'] .= $value;
          break;
        case 'close':
          // end of node, return collected data
          return $children;
          break;
      }
    }
    //return $children;
  }
  /**
  * Appends some values to an array
  *
  * @access    public
  * @param    array    [$target]
  * @param    string    [$key]
  * @param    string    [$value]
  * @param    array    [$attributes]
  * @param    array    [$inner] the children
  * @return    void
  * @since
  */
  function addNode($target, $key, $value = '', $attributes = '', $child = '') {
    if (!isset($target[$key]['value'])  !isset($target[$key][0])) {
      if ($child != '') {
        $target[$key] = $child;
      }
      if ($attributes != '') {
        foreach ($attributes as $k => $v) {
          $target[$key][$k] = $v;
        }
      }
      $target[$key]['value'] = $value;
    } else {
      if (!isset($target[$key][0])) {
        // is string or other
        $oldvalue = $target[$key];
        $target[$key] = array();
        $target[$key][0] = $oldvalue;
        $index = 1;
      } else {
        // is array
        $index = count($target[$key]);
      }
      if ($child != '') {
        $target[$key][$index] = $child;
      }
      if ($attributes != '') {
        foreach ($attributes as $k => $v) {
          $target[$key][$index][$k] = $v;
        }
      }
      $target[$key][$index]['value'] = $value;
    }
    return $target;
  }
  /**
  * Free the resources
  *
  * @access    public
  * @return    void
  **/
  function free() {
    if (isset($this->parser)  is_resource($this->parser)) {
      xml_parser_free($this->parser);
      unset($this->parser);
    }
  }
}

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獲取通過http協議post提交過來xml數據及解析xml
  • php解析xml方法實例詳解
  • php解析xml 的四種簡單方法(附實例)
  • PHP用SAX解析XML的實現代碼與問題分析
  • php遍歷解析xml字符串的方法
  • php 解析xml 的四種方法詳細介紹
  • PHP基于SimpleXML生成和解析xml的方法示例
  • PHP使用xpath解析XML的方法詳解
  • PHP處理數組和XML之間的互相轉換
  • php實現將數組轉換為XML的方法
  • PHP簡單實現解析xml為數組的方法

標簽:曲靖 興安盟 太原 株洲 白酒營銷 南京 南昌 濱州

巨人網絡通訊聲明:本文標題《PHP解析xml格式數據工具類示例》,本文關鍵詞  PHP,解析,xml,格式,數據,工具,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《PHP解析xml格式數據工具類示例》相關的同類信息!
  • 本頁收集關于PHP解析xml格式數據工具類示例的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 日产欧产美一区| 推倒都市极品贵妇| 小柔婬荡高潮呻吟小说| 外国大片又大又好看的PPT| 啊太深了轻点嗯太涨了| 男人女人做性全过程视频高清| 中文字幕羽月希黑人侵犯| 18??????????网站| 国产做受???高潮A| 瑜伽丰满美女按摩三级| 美女和男生在床上| 黄色小说免费观看| 免费欧美黄色电影| 久久久无码精品一区波多野结衣 | 冰漪全套无遮挡图片| 国产精品资源| 美女裸体?开腿羞羞吞精| 欧美激情一区二区A片成人牛牛 | 久久中字| 电影尼禄荒淫史3无删减| 久久深夜视频| 屁股坐脸舌头伸进去| 国产寡妇婬乱一级A片在线直播APP | 秋霞三级完整版在线观看| 午夜欧美福利视频| 怡红影院| 腐文h| 你驾驭不了妾身哦| 国产综合91天堂亚洲国产| 成人欧美一区二区三区黑人| 高跟91娇喘????白丝| 国严产品自偷自偷自偷最新章节| 女人十八毛片一级毛片免费看| 成人精品视频| 天天爽夜夜爽夜夜爽精品视频| 国产精品一区25P| 国产精品一区二区午夜嘿嘿嘿小说| 麻酥酥福利视频| 亚洲成人77777| 亚洲ⅴa欧美va韩国va一线| 藏经阁H文第一版主|