本文实例讲述了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'])) "color: #800000">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,解析,xml格式数据,工具类,解析xml

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
评论“PHP解析xml格式数据工具类示例”
暂无“PHP解析xml格式数据工具类示例”评论...

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。