首页 >后端开发 >php教程 >php 生成xml代码_PHP教程

php 生成xml代码_PHP教程

WBOY
WBOY原创
2016-07-13 10:58:11750浏览

php 生成xml代码class XMLParser {

 function getChildren($vals, &$i) {

  $children = array();
  if(isset($vals[$i]['value'])) {
   $children['VALUE'] = $vals[$i]['value'];
  }

  while(++$i    switch($vals[$i]['type']) {

   case 'cdata':
    if(isset($children['VALUE'])) {
     $children['VALUE'] .= $vals[$i]['value'];
    } else {
     $children['VALUE'] = $vals[$i]['value'];
    }
    break;

   case 'complete':
    if(isset($vals[$i]['attributes'])) {
     $children[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
     $index = count($children[$vals[$i]['tag']]) - 1;

     if(isset($vals[$i]['value'])) {
      $children[$vals[$i]['tag']][$index]['VALUE'] = $vals[$i]['value'];
     } else {
      $children[$vals[$i]['tag']][$index]['VALUE'] = '';
     }
    } else {
     if(isset($vals[$i]['value'])) {
      $children[$vals[$i]['tag']][]['VALUE'] = $vals[$i]['value'];
     } else {
      $children[$vals[$i]['tag']][]['VALUE'] = '';
     }
    }
    break;

   case 'open':
    if(isset($vals[$i]['attributes'])) {
     $children[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
     $index = count($children[$vals[$i]['tag']]) - 1;
     $children[$vals[$i]['tag']][$index] = array_merge($children[$vals[$i]['tag']][$index], $this->getChildren($vals, $i));
    } else {
     $children[$vals[$i]['tag']][] = $this->GetChildren($vals, $i);
    }
    break;

   case 'close':
    return $children;
   }
  }
 }

 function getXMLTree($data) {

  $parser = xml_parser_create('UTF-8');
  xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0);
  xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  xml_parse_into_struct($parser, $data, $vals, $index);
  xml_parser_free($parser);

  $tree = array();
  $i = 0;

  if(isset($vals[$i]['attributes'])) {
   $tree[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
   $index = count($tree[$vals[$i]['tag']]) - 1;
   $tree[$vals[$i]['tag']][$index] =  array_merge($tree[$vals[$i]['tag']][$index], $this->getChildren($vals, $i));
  } else {
   $tree[$vals[$i]['tag']][] = $this->getChildren($vals, $i);
  }
  return $tree;
 }
}

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/632011.htmlTechArticlephp 生成xml代码class XMLParser { function getChildren($vals, $i) { $children = array(); if(isset($vals[$i]['value'])) { $children['VALUE'] = $vals[$i]['value']; } while(++$i co...
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn