视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
PHP解析xml格式数据工具类实例分享
2020-11-27 20:07:02 责编:小采
文档


本文主要介绍了PHP解析xml格式数据工具类,涉及php针对xml格式数据节点添加、获取、解析等相关操作技巧,需要的朋友可以参考下,希望能帮助到大家。

本文实例讲述了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);
 }
 }

下载本文
显示全文
专题