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 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100