基于节点属性值的Echo XML子节点值

时间:2014-09-24 11:55:29

标签: php xml simplexml

我从API获得XML响应,如下所示:

$response = <<<XML
<response uri="/crm/private/xml/Leads/insertRecords">
<result>
  <message>Record(s) updated successfully</message>
  <recorddetail>
    <fl val="Id">1203498000000109001</fl>
    <fl val="Created Time">2014-09-24 09:19:44</fl>
    <fl val="Modified Time">2014-09-24 11:38:08</fl>
    <fl val="Created By"><!--[CDATA[Brydges]]--></fl>
    <fl val="Modified By"><!--[CDATA[Brydges]]--></fl>
  </recorddetail>
</result>
</response>
XML;

如果我想要消息,使用

这似乎很容易

$xml_response = new SimpleXMLElement($response); $message = $xml_response->result->message; echo $message;

但是,我正在尝试检索<fl val="id">行的内容,例如1203498000000109001.

我已经查看过很多关于SO的问题,并尝试了以下所有建议,但没有成功:

//using xpath with SimpleXMLElement
$xml_response =  new SimpleXMLElement($response);
$zoho_id = $xml_response->xpath('/response/result/recorddetail/fl[@val="Id"]');
echo $zoho_id;

//using xpath with simplexml_load_string
$xml_response =  new simplexml_load_string($response);
$zoho_id = $xml_response->xpath('/response/result/recorddetail/fl[@val="Id"]');
echo $zoho_id;

//using a foreach loop
$xml_response = new SimpleXMLElement($response);
foreach ($xml_response->result->recorddetail->fl as $fl) {
 if ((string) $fl['val'] == 'Id') {
    echo (string) $fl;
  }
}

// using a foreach loop and then a switch case over the val attribute value to only echo Id
foreach ($xml_response->result->recorddetail->fl as $fl) {
    switch((string) $fl['val']) { // Get attributes as element indices
    case 'Id':
        echo (string)$fl, ' is the Id';
        break;
    }
}

有关我如何检索我需要的内容的任何建议吗?

关注GHOSTS的建议:

print_r($ doc)返回:

SimpleXMLElement Object ( 
    [@attributes] => Array ( 
        [uri] => /crm/private/xml/Leads/insertRecords 
    ) 
    [result] => SimpleXMLElement Object ( 
        [message] => Record(s) updated successfully 
        [recorddetail] => SimpleXMLElement Object ( 
            [FL] => Array ( 
                [0] => 1203498000000109001 
                [1] => 2014-09-24 09:19:44 
                [2] => 2014-09-24 13:06:37 
                [3] => SimpleXMLElement Object ( 
                    [@attributes] => Array ( 
                        [val] => Created By 
                    ) 
                ) 
                [4] => SimpleXMLElement Object ( 
                    [@attributes] => Array ( 
                        [val] => Modified By 
                    ) 
                ) 
            ) 
        ) 
    ) 
)

1 个答案:

答案 0 :(得分:0)

第一个xpath部分已经正确,xpath结果返回一个数组,所以请将其视为:

$xml_response =  new SimpleXMLElement($response);
$zoho_id = (string) $xml_response->xpath('/response/result/recorddetail/fl[@val="Id"]')[0];
                                                                                    //  ^
echo $zoho_id;
// assuming you're expecting one result

如果您有多个节点,则可以使用循环:

$zoho_id = $xml_response->xpath('/response/result/recorddetail/fl[@val="Id"]');
foreach($zoho_id as $node) {
    $id = (string) $node;
    echo $id;
}

或者:

$xml_response =  new SimpleXMLElement($response);
$recorddetail = $xml_response->xpath('/response/result/recorddetail');
foreach($recorddetail as $node) {
    $id_node_val = (string) $node->FL[0];
    echo $id_node_val;
}