Skip to content

Commit 8e35bb5

Browse files
committed
重写xml解析逻辑
1 parent 0ae1df4 commit 8e35bb5

File tree

4 files changed

+67
-17
lines changed

4 files changed

+67
-17
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"ext-libxml": "*",
2020
"ext-xml": "*",
2121
"symfony/var-dumper": "5.3.3",
22-
"symfony/filesystem": "5.3.3"
22+
"symfony/filesystem": "5.3.3",
23+
"ext-xmlreader": "*"
2324
},
2425
"autoload": {
2526
"psr-4": {

demo/xml.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434
</BODY>
3535
</ROOT>
3636
xml;
37-
$object = $xml->xmlToArray($str);
37+
$object = $xml->xmlToArray($str,null,['PrintTicketInfo']);
3838

39-
40-
$tem = $object->BODY->RSP2006->PrintTicketInfo[0]->TicketTypeDis->__toString();
41-
var_dump($tem);
42-
echo "\n";
43-
var_dump($object);
39+
//
40+
// $tem = $object->BODY->RSP2006->PrintTicketInfo[0]->TicketTypeDis->__toString();
41+
// var_dump($tem);
42+
// echo "\n";
43+
// var_dump($object);

src/siam/Api.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ public static function send($code, $data = [], $msg = '')
1212
'msg' => $msg,
1313
];
1414

15-
$json = json_encode($return, 256);
16-
17-
return $json;
15+
return json_encode($return, 256);
1816
}
1917
/**
2018
* 输出调试

src/siam/Xml.php

+58-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212
use Siam\Component\Singleton;
13+
use SimpleXMLElement;
1314

1415
class Xml
1516
{
@@ -38,16 +39,66 @@ function arrayToXml($data, $root = true)
3839
return $str;
3940
}
4041

42+
public function toArray($simpleXMLElement, $list_field_setting = []): array
43+
{
44+
$return = [];
45+
/**
46+
* @var $key
47+
* @var SimpleXMLElement $value
48+
*/
49+
foreach ($simpleXMLElement as $key => $value){
50+
if ($value->count()){
51+
$temp = $this->toArray($value, $list_field_setting);
52+
if (in_array($key, $list_field_setting)){
53+
$return[$key][] = $temp;
54+
}else{
55+
$return[$key] = $temp;
56+
}
57+
}else{
58+
$return[$key] = $value->__toString();
59+
}
60+
}
61+
return $return;
62+
}
4163

42-
function xmlToArray($xml)
64+
public function xmlToArray($xml_string, $list_identify = null, $list_field_setting = []): array
4365
{
44-
// 清理替换没有闭合标签的
45-
$xml = preg_replace('/\<(\w+)\/\>/','<$1></$1>',$xml);
66+
$list = [];
67+
68+
if (!$list_identify){
69+
$list = $this->toArray(simplexml_load_string($xml_string), $list_field_setting);
70+
}else{
71+
// 加载XML内容
72+
$xml_reader = new \XMLReader();
73+
$xml_reader->xml($xml_string);
74+
// move the pointer to the first product
75+
while ($xml_reader->read() && $xml_reader->name != $list_identify);
76+
77+
// loop through the products
78+
while ($xml_reader->name == $list_identify)
79+
{
80+
// load the current xml element into simplexml and we’re off and running!
81+
$xml = simplexml_load_string($xml_reader->readOuterXML());
82+
83+
// // 读取dom和值
84+
// var_dump($xml);
85+
//
86+
// // 读取dom的属性
87+
// foreach ($xml->city->attributes() as $key => $value){
88+
// var_dump($key);
89+
// var_dump($value->__toString());
90+
// }
91+
92+
$list[] = $this->toArray($xml, $list_field_setting);
93+
94+
$xml_reader->next($list_identify);
95+
}
96+
97+
// don’t forget to close the file
98+
$xml_reader->close();
99+
}
46100

47-
$object = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
48-
$array = json_decode(json_encode($object),true);
49-
$array = $this->_checkType($array);
50-
return $array;
101+
return $list;
51102
}
52103

53104
/**

0 commit comments

Comments
 (0)