php官方网---php解析xml文件
2011-11-13 17:22
465 查看
<?php header('Content-Type: text/html; charset=utf-8;'); /*--------------------------------- * @PHP解析XML基本操作 * 解析xml ----------------------------------*/ $xml = ereg_replace('[\\/]{1,}', '/', dirname(__FILE__).'/bbc.xml'); $dom = new DOMDocument(); $dom->load($xml); $item = $dom->getElementsByTagName('item'); $titlearr = array(); foreach($item as $sub){ //$nodevalue[] = iconv('UTF-8', 'GBK', $value->nodeValue); $title = $sub->getElementsByTagName('title'); $link = $sub->getElementsByTagName('link'); foreach($title as $val){ $titlearr[] = $val->nodeValue; } foreach($link as $val){ $linkarr[] = $val->nodeValue; } } //var_dump(array_unique($linkarr)); $result = array(); for($i=0; $i<count($titlearr); $i++){ $array = array(); $array = array('title'=>$titlearr[$i], 'link'=>$linkarr[$i]); $result[] = $array; } //var_dump($result); /** * Convert an xml file to an associative array (including the tag attributes): * * @param Str $xml file/string. */ class xmlToArrayParser { /** * The array created by the parser which can be assigned to a variable with: $varArr = $domObj->array. * * @var Array */ public $array; private $parser; private $pointer; /** * $domObj = new xmlToArrayParser($xml); * * @param Str $xml file/string */ public function __construct($xml) { $this->pointer =& $this->array; $this->parser = xml_parser_create("UTF-8"); xml_set_object($this->parser, $this); xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false); xml_set_element_handler($this->parser, "tag_open", "tag_close"); xml_set_character_data_handler($this->parser, "cdata"); xml_parse($this->parser, ltrim($xml)); } private function tag_open($parser, $tag, $attributes) { $this->convert_to_array($tag, '_'); $idx=$this->convert_to_array($tag, 'cdata'); if(isset($idx)) { $this->pointer[$tag][$idx] = Array('@idx' => $idx,'@parent' => &$this->pointer); $this->pointer =& $this->pointer[$tag][$idx]; }else { $this->pointer[$tag] = Array('@parent' => &$this->pointer); $this->pointer =& $this->pointer[$tag]; } if (!empty($attributes)) { $this->pointer['_'] = $attributes; } } /** * Adds the current elements content to the current pointer[cdata] array. */ private function cdata($parser, $cdata) { if(isset($this->pointer['cdata'])) { $this->pointer['cdata'] .= $cdata;} else { $this->pointer['cdata'] = $cdata;} } private function tag_close($parser, $tag) { $current = & $this->pointer; if(isset($this->pointer['@idx'])) {unset($current['@idx']);} $this->pointer = & $this->pointer['@parent']; unset($current['@parent']); if(isset($current['cdata']) && count($current) == 1) { $current = $current['cdata'];} else if(empty($current['cdata'])) { unset($current['cdata']); } } /** * Converts a single element item into array(element[0]) if a second element of the same name is encountered. */ private function convert_to_array($tag, $item) { if(isset($this->pointer[$tag][$item])) { $content = $this->pointer[$tag]; $this->pointer[$tag] = array((0) => $content); $idx = 1; }else if (isset($this->pointer[$tag])) { $idx = count($this->pointer[$tag]); if(!isset($this->pointer[$tag][0])) { foreach ($this->pointer[$tag] as $key => $value) { unset($this->pointer[$tag][$key]); $this->pointer[$tag][0][$key] = $value; }}}else $idx = null; return $idx; } } $xmldata = file_get_contents($xml); $myxml = new xmlToArrayParser($xmldata); var_dump($myxml); ?>
参考网址: http://cn2.php.net/manual/zh/ref.xml.php
相关文章推荐
- 解读PHP DOMDocument在解析XML文件中的作用
- PHP通过DOM解析XML文件或者xml字符串
- 解读PHP DOMDocument在解析XML文件中的作用
- php的SimpleXML方法读写XML接口文件实例解析
- php解析xml文件到一个数组
- PHP中XPATH 实现xml及html文件快速解析(附xml做小型数据库实现六级单词快速查询实例)
- Php 解析XML文件
- php的SimpleXML方法读写XML接口文件实例解析
- PHP SimpleXMLElement:XML文件解析和读写
- php无法解析post接收过来的xml文件
- php解析xml文件
- 使用PHP DOM-XML创建和解析XML文件
- 解读PHP DOMDocument在解析XML文件中的作用
- PHP使用SimpleElement创建和解析xml文件
- PHP解析Android APK包的XML文件
- php的curl,uploadfile上传文件,xml解析,邮件发送,动态时间,去重,等
- php的SimpleXML方法读写XML接口文件实例解析
- PHP解析Android APK包的XML文件
- Php 解析XML文件
- php解析xml文件