| <?php | 
|   | 
| /** | 
|  * 解析xml, 需调用 IsSuccess 判断是否成功解析 | 
|  */ | 
| class XMLParse | 
| { | 
|   | 
|     private $ok; | 
|     private $vals; | 
|     private $index; | 
|   | 
|     /** | 
|      * @param string $data 要解析的xml | 
|      */ | 
|     function __construct($data) | 
|     { | 
|         $p = xml_parser_create(); | 
|         xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0); // case-folding 就是转换为大写,默认是开启的,这里关闭 | 
|         $this->ok = xml_parse_into_struct($p, $data, $this->vals, $this->index); | 
|         if ($this->ok != 1) { | 
|             \Logging\LogError("XMLParse error. data:" . $data); | 
|         } | 
|         xml_parser_free($p); | 
|     } | 
|   | 
|     public function IsSuccess() | 
|     { | 
|         return $this->ok == 1; | 
|     } | 
|   | 
|     public function GetValue($key) | 
|     { | 
|         return $this->vals[$this->index[$key][0]]["value"]; | 
|     } | 
|   | 
|     public function GetValues() | 
|     { | 
|         $kv = array(); | 
|         foreach ($this->vals as $value) { | 
|             if (array_key_exists('value', $value)) { | 
|                 $kv[$value['tag']] = $value['value']; | 
|             } | 
|         } | 
|         return $kv; | 
|     } | 
| } |