PHP解析xml四种方法 另附上一解析类

来源:互联网 发布:光信息科学与技术知乎 编辑:程序博客网 时间:2024/05/01 23:19
XML处理是开发过程中经常遇到的,PHP对其也有很丰富的支持,本文只是对其中某几种解析技术做简要说明,包括:Xml parser, SimpleXML, XMLReader, DOMDocument。

1。 XML Expat Parser:

XML Parser使用Expat XML解析器。Expat是一种基于事件的解析器,它把XML文档视为一系列事件。当某个事件发生时,它调用一个指定的函数处理它。Expat是无验证的解析器,忽略任何链接到文档的DTD。但是,如果文档的形式不好,则会以一个错误消息结束。由于它基于事件,且无验证,Expat具有快速并适合web应用程序的特性。

XML Parser的优势是性能好,因为它不是将整个xml文档载入内存后再处理,而是边解析边处理。但也正因为如此,它不适合那些要对xml结构做动态调整、或基于xml上下文结构做复杂操作的需求。如果你只是要解析处理一个结构良好的xml文档,那么它可以很好的完成任务。需要注意的是XML Parser只支持三种编码格式:US-ASCII, ISO-8859-1和UTF-8,如果你的xml数据是其他编码,需要先转换成以上三个之一。
XML Parser常用的解析方式大体有两种(其实就是两个函数):xml_parse_into_struct和xml_set_element_handler。

xml_parse_into_struct

此方法是将xml数据解析到两个数组中:
index数组——包含指向Value 数组中值的位置的指针
value数组——包含来自被解析的 XML 的数据

这俩数组文字描述起来有点麻烦,还是看个例子吧(来自php官方文档)

$simple = "<para><note>simple note</note></para>";
$p = xml_parser_create();
xml_parse_into_struct($p, $simple, $vals, $index);
xml_parser_free($p);
echo "Index array\n";
print_r($index);
echo "\nVals array\n";
print_r($vals);


输出:
Index array
Array
(
    [PARA] => Array
        (
            [0] => 0
            [1] => 2
        )

    [NOTE] => Array
        (
            [0] => 1
        )
)

Vals array
Array
(
    [0] => Array
        (
            [tag] => PARA
            [type] => open
            [level] => 1
        )

    [1] => Array
        (
            [tag] => NOTE
            [type] => complete
            [level] => 2
            [value] => simple note
        )

    [2] => Array
        (
            [tag] => PARA
            [type] => close
            [level] => 1
        )
)

其中index数组以标签名为key,对应的值是一个数组,里面包括所有此标签在value数组中的位置。然后通过这个位置,找到此标签对应的值。

如果xml中每组数据格式有出入,不能做到完全统一,那么在写代码时要注意,说不定就得到了错误的结果。比如下面这个例子:

$xml = '
<infos>
<para><note>note1</note><extra>extra1</extra></para>
<para><note>note2</note></para>
<para><note>note3</note><extra>extra3</extra></para>
</infos>
';

$p = xml_parser_create();
xml_parse_into_struct($p, $xml, $values, $tags);
xml_parser_free($p);
$result = array();
//下面的遍历方式有bug隐患
for ($i=0; $i<3; $i++) {
  $result[$i] = array();
  $result[$i]["note"] = $values[$tags["NOTE"][$i]]["value"];
  $result[$i]["extra"] = $values[$tags["EXTRA"][$i]]["value"];
}
print_r($result);


要是按照上面那种方式遍历,看似代码简单,但是暗藏危机,最致命的是得到错误的结果(extra3跑到第二个para里了)。所以要以一种比较严谨的方式遍历:

$result = array();
$paraTagIndexes = $tags['PARA'];
$paraCount = count($paraTagIndexes);
for($i = 0; $i < $paraCount; $i += 2) {
  $para = array();
  //遍历para标签对之间的所有值
  for($j = $paraTagIndexes[$i]; $j < $paraTagIndexes[$i+1]; $j++) {
    $value = $values[$j]['value'];
    if(empty($value)) continue;

    $tagname = strtolower($values[$j]['tag']);
    if(in_array($tagname, array('note','extra'))) {
      $para[$tagname] = $value;
    }
  }
  $result[] = $para;
}


其实我很少用xml_parse_into_struct函数,所以上面所谓“严谨”的代码保不齐还会有其他情况下的bug。- -|
xml_set_element_handler

这种方式是为parser设置处理元素起始、元素终止的回调函数。配套的还有xml_set_character_data_handler用来为parser设置数据的回调函数。这种方式写的代码比较清晰,利于维护。

Example:

$xml = <<<XML
<infos>
<para><note>note1</note><extra>extra1</extra></para>
<para><note>note2</note></para>
<para><note>note3</note><extra>extra3</extra></para>
</infos>
XML;

$result = array();
$index = -1;
$currData;

function charactor($parser, $data) {
  global $currData;
  $currData = $data;
}

function startElement($parser, $name, $attribs) {
  global $result, $index;
  $name = strtolower($name);
  if($name == 'para') {
    $index++;
    $result[$index] = array();
  }
}

function endElement($parser, $name) {
  global $result, $index, $currData;
  $name = strtolower($name);
  if($name == 'note' || $name == 'extra') {
    $result[$index][$name] = $currData;
  }
}

$xml_parser = xml_parser_create();
xml_set_character_data_handler($xml_parser, "charactor");
xml_set_element_handler($xml_parser, "startElement", "endElement");
if (!xml_parse($xml_parser, $xml)) {
  echo "Error when parse xml: ";
  echo xml_error_string(xml_get_error_code($xml_parser));
}
xml_parser_free($xml_parser);

print_r($result);


可见,set handler方式虽然代码行数多,但思路清晰,可读性更好,不过性能上略慢于第一种方式,而且灵活性不强。XML Parser支持PHP4,适用于于使用老版本的系统。对于PHP5环境,还是优先考虑下面的方法吧。

2。 SimpleXML

SimpleXML是PHP5后提供的一套简单易用的xml工具集,可以把xml转换成方便处理的对象,也可以组织生成xml数据。不过它不适用于包含namespace的xml,而且要保证xml格式完整(well-formed)。它提供了三个方法:simplexml_import_dom、simplexml_load_file、simplexml_load_string,函数名很直观地说明了函数的作用。三个函数都返回SimpleXMLElement对象,数据的读取/添加都是通过SimpleXMLElement操作。

$string = <<<XML
<?xml version='1.0'?>
<document>
  <cmd>login</cmd>
  <login>imdonkey</login>
</document>
XML;

$xml = simplexml_load_string($string);
print_r($xml);
$login = $xml->login;//这里返回的依然是个SimpleXMLElement对象
print_r($login);
$login = (string) $xml->login;//在做数据比较时,注意要先强制转换
print_r($login);


SimpleXML的优点是开发简单,缺点是它会将整个xml载入内存后再进行处理,所以在解析超多内容的xml文档时可能会力不从心。如果是读取小文件,而且xml中也不包含namespace,那SimpleXML是很好的选择。

 

3。 XMLReader

XMLReader也是PHP5之后的扩展(5.1后默认安装),它就像游标一样在文档流中移动,并在每个节点处停下来,操作起来很灵活。它提供了对输入的快速和非缓存的流式访问,可以读取流或文档,使用户从中提取数据,并跳过对应用程序没有意义的记录。
以一个利用google天气api获取信息的例子展示下XMLReader的使用,这里也只涉及到一小部分函数,更多还请参考官方文档。

$xml_uri = 'http://www.google.com/ig/api?weather=Beijing&hl=zh-cn';
$current = array();
$forecast = array();

$reader = new XMLReader();
$reader->open($xml_uri, 'gbk');
while ($reader->read()) {
  //get current data
  if ($reader->name == "current_conditions" && $reader->nodeType == XMLReader::ELEMENT) {
    while($reader->read() && $reader->name != "current_conditions") {
      $name = $reader->name;
      $value = $reader->getAttribute('data');
      $current[$name] = $value;
    }
  }

  //get forecast data
  if ($reader->name == "forecast_conditions" && $reader->nodeType == XMLReader::ELEMENT) {
    $sub_forecast = array();
    while($reader->read() && $reader->name != "forecast_conditions") {
      $name = $reader->name;
      $value = $reader->getAttribute('data');
      $sub_forecast[$name] = $value;
    }
    $forecast[] = $sub_forecast;
  }
}
$reader->close();


XMLReader和XML Parser类似,都是边读边操作,较大的差异在于SAX模型是一个“推送”模型,其中分析器将事件推到应用程序,在每次读取新节点时通知应用程序,而使用XmlReader的应用程序可以随意从读取器提取节点,可控性更好。
由于XMLReader基于libxml,所以有些函数要参考文档看看是否适用于你的libxml版本。

4。 DOMDocument

DOMDocument还是PHP5后推出的DOM扩展的一部分,可用来建立或解析html/xml,目前只支持utf-8编码。

$xmlstring = <<<XML
<?xml version='1.0'?>
<document>
  <cmd attr='default'>login</cmd>
  <login>imdonkey</login>
</document>
XML;

$dom = new DOMDocument();
$dom->loadXML($xmlstring);
print_r(getArray($dom->documentElement));

function getArray($node) {
  $array = false;

  if ($node->hasAttributes()) {
    foreach ($node->attributes as $attr) {
      $array[$attr->nodeName] = $attr->nodeValue;
    }
  }

  if ($node->hasChildNodes()) {
    if ($node->childNodes->length == 1) {
      $array[$node->firstChild->nodeName] = getArray($node->firstChild);
    } else {
      foreach ($node->childNodes as $childNode) {
      if ($childNode->nodeType != XML_TEXT_NODE) {
        $array[$childNode->nodeName][] = getArray($childNode);
      }
    }
  }
  } else {
    return $node->nodeValue;
  }
  return $array;
}


从函数名上看感觉跟JavaScript很像,应该是借鉴了一些吧。DOMDocument也是一次性将xml载入内存,所以内存问题同样需要注意。PHP提供了这么多的xml处理方式,开发人员在选择上就要花些时间了解,选择适合项目需求及系统环境、又便于维护的方法。


http://www.phpzh.com/archives/525/2


附:

解析xml:

  1. <?php   
  2. ###################################################################################   
  3. #   
  4. # XML Library, by Keith Devens, version 1.2b   
  5. # <a href="http://keithdevens.com/software/phpxml" target="_blank">http://keithdevens.com/software/phpxml</a>   
  6. #   
  7. # This code is Open Source, released under terms similar to the Artistic License.   
  8. # Read the license at <a href="http://keithdevens.com/software/license" target="_blank">http://keithdevens.com/software/license</a>   
  9. #   
  10. ###################################################################################   
  11.   
  12. ###################################################################################   
  13. # XML_unserialize: takes raw XML as a parameter (a string)   
  14. and returns an equivalent PHP data structure   
  15. ###################################################################################   
  16. function & XML_unserialize(&$xml){   
  17.     $xml_parser = &new XML();   
  18.     $data = &$xml_parser->parse($xml);   
  19.     $xml_parser->destruct();   
  20.     return $data;   
  21. }   
  22. ###################################################################################   
  23. # XML_serialize: serializes any PHP data structure into XML   
  24. # Takes one parameter: the data to serialize. Must be an array.   
  25. ###################################################################################   
  26. function & XML_serialize(&$data$level = 0, $prior_key = NULL){   
  27.     if($level == 0){ ob_start(); echo '<?xml version="1.0" ?>',"\n"; }   
  28.     while(list($key$value) = each($data))   
  29.         if(!strpos($key' attr')) #if it's not an attribute  
  30.             #we don't treat attributes by themselves, so for an emptyempty element   
  31.             # that has attributes you still need to set the element to NULL   
  32.   
  33.             if(is_array($valueand array_key_exists(0, $value)){   
  34.                 XML_serialize($value$level$key);   
  35.             }else{   
  36.                 $tag = $prior_key ? $prior_key : $key;   
  37.                 echo str_repeat("\t"$level),'<',$tag;   
  38.                 if(array_key_exists("$key attr"$data)){ #if there's an attribute for this element  
  39.                     while(list($attr_name, $attr_value) = each($data["$key attr"]))  
  40.                         echo ' ',$attr_name,'="',htmlspecialchars($attr_value),'"';  
  41.                     reset($data["$key attr"]);  
  42.                 }  
  43.  
  44.                 if(is_null($value)) echo " />\n";  
  45.                 elseif(!is_array($value)) echo '>',htmlspecialchars($value),"</$tag>\n";  
  46.                 else echo ">\n",XML_serialize($value, $level+1),str_repeat("\t", $level),"</$tag>\n";  
  47.             }  
  48.     reset($data);  
  49.     if($level == 0){ $str = &ob_get_contents(); ob_end_clean(); return $str; }  
  50.  
  51. ###################################################################################  
  52. # XML class: utility class to be used with PHP's XML handling functions   
  53. ###################################################################################   
  54. class XML{   
  55.     var $parser;   #a reference to the XML parser   
  56.     var $document; #the entire XML structure built up so far   
  57.     var $parent;   #a pointer to the current parent - the parent will be an array   
  58.     var $stack;    #a stack of the most recent parent at each nesting level   
  59.     var $last_opened_tag; #keeps track of the last tag opened.   
  60.   
  61.     function XML(){   
  62.          $this->parser = &xml_parser_create();   
  63.         xml_parser_set_option(&$this->parser, XML_OPTION_CASE_FOLDING, false);   
  64.         xml_set_object(&$this->parser, &$this);   
  65.         xml_set_element_handler(&$this->parser, 'open','close');   
  66.         xml_set_character_data_handler(&$this->parser, 'data');   
  67.     }   
  68.     function destruct(){ xml_parser_free(&$this->parser); }   
  69.     function & parse(&$data){   
  70.         $this->document = array();   
  71.         $this->stack    = array();   
  72.         $this->parent   = &$this->document;   
  73.         return xml_parse(&$this->parser, &$data, true) ? $this->document : NULL;   
  74.     }   
  75.     function open(&$parser$tag$attributes){   
  76.         $this->data = ''; #stores temporary cdata   
  77.         $this->last_opened_tag = $tag;   
  78.         if(is_array($this->parent) and array_key_exists($tag,$this->parent)){ #if you've seen this tag before  
  79.             if(is_array($this->parent[$tag]) and array_key_exists(0,$this->parent[$tag])){ #if the keys are numeric  
  80.                 #this is the third or later instance of $tag we've come across   
  81.                 $key = count_numeric_items($this->parent[$tag]);   
  82.             }else{   
  83.                 #this is the second instance of $tag that we've seen. shift around  
  84.                 if(array_key_exists("$tag attr",$this->parent)){  
  85.                     $arr = array('0 attr'=>&$this->parent["$tag attr"], &$this->parent[$tag]);  
  86.                     unset($this->parent["$tag attr"]);  
  87.                 }else{  
  88.                     $arr = array(&$this->parent[$tag]);  
  89.                 }  
  90.                 $this->parent[$tag] = &$arr;  
  91.                 $key = 1;  
  92.             }  
  93.             $this->parent = &$this->parent[$tag];  
  94.         }else{  
  95.             $key = $tag;  
  96.         }  
  97.         if($attributes) $this->parent["$key attr"] = $attributes;  
  98.         $this->parent  = &$this->parent[$key];  
  99.         $this->stack[] = &$this->parent;  
  100.     }  
  101.     function data(&$parser, $data){  
  102.         if($this->last_opened_tag != NULL) #you don't need to store whitespace in between tags   
  103.             $this->data .= $data;   
  104.     }   
  105.     function close(&$parser$tag){   
  106.         if($this->last_opened_tag == $tag){   
  107.             $this->parent = $this->data;   
  108.             $this->last_opened_tag = NULL;   
  109.         }   
  110.         array_pop($this->stack);   
  111.         if($this->stack) $this->parent = &$this->stack[count($this->stack)-1];   
  112.     }   
  113. }   
  114. function count_numeric_items(&$array){   
  115.     return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0;   
  116. }   
  117. ?>  


原创粉丝点击