dopgl80062 2018-02-16 07:10
浏览 48
已采纳

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

I have problem in code when i parse the XML to Array.it returns some tags and not the complete tags.I want to get all tags inside in soap response.I have xml file.and upload this file.

Here is data.txt file below:

<?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>

My PHP code:

<?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);
?>

My output:

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

My expected output should:

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

Please help

Thanks in Advance.

  • 写回答

1条回答 默认 最新

  • dongwei9771 2018-02-16 07:31
    关注

    The problem is caused by simple_html_dom trying to correct your XML. There are a few issues with it and if they were resolved, you could load it much more efficiently with either DOMDocument or SimpleXML.

    If you corrected the element

    <name>testing</imran>
    

    as the html parser is trying to correct the structure, it's taking this and the <cnic> element as one piece of data. Change this to

    <name>testing</name>
    

    and your output changes to...

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

    If you corrected your XML, then you could do the following (XML included)...

    $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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧