dozug64282 2016-08-04 11:30
浏览 16
已采纳

从XML获取特定项目

I searched the web and the stackoverflow archive and haven't found anything that really helped me solve this problem.

I have the following XML file

<?xml version="1.0"?>
<Results>
  <Result>
    <Step>001</Step>
    <State>1</State>
    <Complete>true</Complete>
  </Result>
    <Result>
        <Step>002</Step>
        <State>1</State>
        <Complete>true</Complete>
    </Result>
    <Result>
        <Step>003</Step>
        <State>1</State>
        <Complete>false</Complete>
    </Result>
</Results>

And I want to get the value of Complete with the help of Step. So basically, the Client calls getSuccess(param) where param is an entered Step e.g. 002 . I then want to get the value of this specific Array (or Result tree). My Code looks like this right now

public function getSuccess($step){

    $doc = new DOMDocument('1.0');
    $doc->formatOutput = true;
    $doc->Load("./API/response.xml");

    // Borat: >>Great Success!<<
    $Borat = $doc->getElementsByTagName('Result');
    foreach ($Borat as $Result) {
        ###########
        #CODE HERE#
        ###########
    }
}

I am now looking for the exact code to echo the result.

Thanks in advance. And if there exists a topic that solved my problem, please let me know and I will close this one.

  • 写回答

2条回答 默认 最新

  • dszdiavv474681 2016-08-04 12:42
    关注

    You can compare step in IF condition and get any node of XML you want.

    For Example: I am getting 'State' node here.

    function getSuccess($step) {
    
        if (file_exists("./response.xml")) {
            $xml = simplexml_load_file("./response.xml");
    
            foreach ($xml as $Result) {
    
                if ($step == $Result->Step) {
                    $State = $Result->State;
                    return $State;
                }
            }
        } else {
            exit('Failed to open test.xml.');
        }
    
    }
    
    echo getSuccess('001');
    

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Matlab在app上输入带有矩阵形式的初始条件发生错误
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 用ADS设计一款的射频功率放大器