hxp
2025-06-09 6c3f6335c70859ded94a1ad8d218acb0ac34239c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?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;
    }
}