Skip to content

Commit

Permalink
重写xml解析逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
xuanyanwow committed Nov 19, 2021
1 parent 0ae1df4 commit 8e35bb5
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 17 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"ext-libxml": "*",
"ext-xml": "*",
"symfony/var-dumper": "5.3.3",
"symfony/filesystem": "5.3.3"
"symfony/filesystem": "5.3.3",
"ext-xmlreader": "*"
},
"autoload": {
"psr-4": {
Expand Down
12 changes: 6 additions & 6 deletions demo/xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
</BODY>
</ROOT>
xml;
$object = $xml->xmlToArray($str);
$object = $xml->xmlToArray($str,null,['PrintTicketInfo']);


$tem = $object->BODY->RSP2006->PrintTicketInfo[0]->TicketTypeDis->__toString();
var_dump($tem);
echo "\n";
var_dump($object);
//
// $tem = $object->BODY->RSP2006->PrintTicketInfo[0]->TicketTypeDis->__toString();
// var_dump($tem);
// echo "\n";
// var_dump($object);
4 changes: 1 addition & 3 deletions src/siam/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ public static function send($code, $data = [], $msg = '')
'msg' => $msg,
];

$json = json_encode($return, 256);

return $json;
return json_encode($return, 256);
}
/**
* 输出调试
Expand Down
65 changes: 58 additions & 7 deletions src/siam/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@


use Siam\Component\Singleton;
use SimpleXMLElement;

class Xml
{
Expand Down Expand Up @@ -38,16 +39,66 @@ function arrayToXml($data, $root = true)
return $str;
}

public function toArray($simpleXMLElement, $list_field_setting = []): array
{
$return = [];
/**
* @var $key
* @var SimpleXMLElement $value
*/
foreach ($simpleXMLElement as $key => $value){
if ($value->count()){
$temp = $this->toArray($value, $list_field_setting);
if (in_array($key, $list_field_setting)){
$return[$key][] = $temp;
}else{
$return[$key] = $temp;
}
}else{
$return[$key] = $value->__toString();
}
}
return $return;
}

function xmlToArray($xml)
public function xmlToArray($xml_string, $list_identify = null, $list_field_setting = []): array
{
// 清理替换没有闭合标签的
$xml = preg_replace('/\<(\w+)\/\>/','<$1></$1>',$xml);
$list = [];

if (!$list_identify){
$list = $this->toArray(simplexml_load_string($xml_string), $list_field_setting);
}else{
// 加载XML内容
$xml_reader = new \XMLReader();
$xml_reader->xml($xml_string);
// move the pointer to the first product
while ($xml_reader->read() && $xml_reader->name != $list_identify);

// loop through the products
while ($xml_reader->name == $list_identify)
{
// load the current xml element into simplexml and we’re off and running!
$xml = simplexml_load_string($xml_reader->readOuterXML());

// // 读取dom和值
// var_dump($xml);
//
// // 读取dom的属性
// foreach ($xml->city->attributes() as $key => $value){
// var_dump($key);
// var_dump($value->__toString());
// }

$list[] = $this->toArray($xml, $list_field_setting);

$xml_reader->next($list_identify);
}

// don’t forget to close the file
$xml_reader->close();
}

$object = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$array = json_decode(json_encode($object),true);
$array = $this->_checkType($array);
return $array;
return $list;
}

/**
Expand Down

0 comments on commit 8e35bb5

Please sign in to comment.