如何从 XML(fast info document) 在 PHP 中获取所有子标签

How to get all children tags from XML(fast info document) in PHP

当我将 XML 解析为 Array.it returns 一些标签而不是完整的 tags.I 时,我的代码出现问题 tags.I 想在 soap 中获取所有标签 response.I xml file.and 上传此文件。

下面是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       
        )
)

请帮忙

提前致谢。

此问题是由于 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);