如何在PHP中从XML(快速信息文档)中获取所有子标签

时间:2018-02-16 07:10:36

标签: php xml simple-html-dom

当我将XML解析为Array时,我在代码中遇到问题。返回一些标签而不是完整的标签。我想在soap响应中获取所有标签。我有xml文件。并上传此文件。

以下是data.txt文件:

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="<a rel="nofollow" class="external free" href="http://schemas.xmlsoap.org/soap/envelope/">http://schemas.xmlsoap.org/soap/envelope/</a>"
  xmlns="urn:enterprise.soap.sforce.com">
  <soapenv:Body>
     <retrieveResponse>
        <result xsi:type="sf:sObject">
           <id>123</id>
           <description>description</description>
           <name>testing</imran>
           <cnic>23198398213</cnic>
        </result>
     </retrieveResponse>
  </soapenv:Body>
</soapenv:Envelope>

我的PHP代码:

<?php
    ini_set("memory_limit", "44879M");
    include("dom.php");
    $xml = str_get_html( file_get_contents("data.txt") );
    $final = array();
    $result = $xml->find("result");
    foreach($result as $r){

        $tag = $r->children();
        $one = array();
        foreach($tag as $child){
            $tag = $child->tag;
            echo "<pre>";
            print_r($tag); echo "<br>";

            if( stristr($tag, ":") ){
                list($com, $tag) = explode(":", $tag);

            }
            $one[$tag]  =  trim(strip_tags($child->innertext));
        }
        $final[] = $one;
        //print_r($final); exit;
    }
    print_r($final);
?>

我的输出:

id
description
name
Array
(
    [0] => Array
        (
            [id] => 123
            [description] => description
            [name] => testing             23198398213
        )
)

我的预期输出应为:

id
description
name
cnic
Array
(
    [0] => Array
        (
            [id] => 123
            [description] => description
            [name] => testing   
            [cnic] =>   23198398213       
        )
)

请帮忙

先谢谢。

1 个答案:

答案 0 :(得分:0)

问题是由simple_html_dom尝试更正XML引起的。它有一些问题,如果它们得到解决,你可以使用DOMDocument或SimpleXML更有效地加载它。

如果您更正了元素

<name>testing</imran>

当html解析器试图纠正结构时,它将此<cnic>元素作为一个数据。将其更改为

<name>testing</name>

,您的输出更改为...

<pre>id<br><pre>description<br><pre>name<br><pre>cnic<br>Array
(
    [0] => Array
        (
            [id] => 123
            [description] => description
            [name] => testing
            [cnic] => 23198398213
        )

)

如果您更正了XML,那么您可以执行以下操作(包括XML)...

$data = <<< XML
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
    xmlns="urn:enterprise.soap.sforce.com">
  <soapenv:Body>
     <retrieveResponse>
        <result xsi:type="sf:sObject">
           <id>123</id>
           <description>description</description>
           <name>testing</name>
           <cnic>23198398213</cnic>
        </result>
     </retrieveResponse>
  </soapenv:Body>
</soapenv:Envelope>
XML;

$xml = new SimpleXMLElement($data);
$xml->registerXPathNamespace("def", "urn:enterprise.soap.sforce.com");
$result = $xml->xpath("//def:result");
$final = array();
foreach ( $result[0]->children() as $element ) {
    $final [ $element->getName() ] = (string)$element;
}
print_r($final);