diff --git a/composer.json b/composer.json index 88b118c..b2d2da0 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/demo/xml.php b/demo/xml.php index 98684df..c408fab 100644 --- a/demo/xml.php +++ b/demo/xml.php @@ -34,10 +34,10 @@ 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); diff --git a/src/siam/Api.php b/src/siam/Api.php index 0debae2..2b2ac3f 100644 --- a/src/siam/Api.php +++ b/src/siam/Api.php @@ -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); } /** * 输出调试 diff --git a/src/siam/Xml.php b/src/siam/Xml.php index 9a8aad9..9eb06bf 100644 --- a/src/siam/Xml.php +++ b/src/siam/Xml.php @@ -10,6 +10,7 @@ use Siam\Component\Singleton; +use SimpleXMLElement; class Xml { @@ -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>',$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; } /**