如果它具有1个diff子名称并且其余值相等,则从X​​ML检索节点值

时间:2013-08-16 07:44:12

标签: php xml

我有一个像这样的XML文件

 <report>
    <table>
      <columns>
         <column name="dateTime"/>
         <column name="userLogin"/>
         <column name="campaignName"/>
         <column name="adGroupName"/>
         <column name="changes"/>
      </columns>
      <row dateTime="Aug 13, 2013 11:56:34 PM" userLogin="hello123" campaignName="Search Exact Parts - 2012 USA" adGroupName="12 volt razor battery" changes="Text ad changed  Status changed from Enabled to Paused"/>
      <row dateTime="Aug 13, 2013 11:56:34 PM" userLogin="hello123" campaignName="Search Exact Parts - 2012 USA" adGroupName="Razor Quad Battery" changes="Text ad changed Status changed from Enabled to Paused"/>
      <row dateTime="Aug 13, 2013 11:56:34 PM" userLogin="hello123" campaignName="Search Exact Parts - 2012 USA" adGroupName="Razor Quad Tires" changes="Text ad changed Status changed from Enabled to Paused"/>
      <row dateTime="Aug 13, 2013 11:56:34 PM" userLogin="hello123" campaignName="Search Exact Parts - 2012 USA" adGroupName="Razor Replacement Battery" changes="Text ad changed Status changed from Enabled to Paused"/>
      <row dateTime="Aug 13, 2013 11:56:34 PM" userLogin="hello123" campaignName="Search Exact Parts - 2012 USA" adGroupName="Razor.com" changes="Text ad changed Status changed from Enabled to Paused"/>
    </table>
  </report>

我的问题是我正在尝试阅读此XML文件。我写的代码是,

foreach($xml->children() as $secgen=>$value){
        foreach($value->columns->children() as $thrgen){
            $feilds[] = $thrgen['name'];
        }
    $data[] = $value->row['dateTime'];
}

此代码仅读取第一行值,即“2013年8月13日11:56:34 PM”

即使我以所有可能的方式尝试过我所知道的。但我无法以正确的方式检索方式。如果有人能解决问题,我会非常感激。

1 个答案:

答案 0 :(得分:0)

使用xpath选择所有datetime个节点的<row>属性:

$results = $xml->xpath("//row/@dateTime");

全部见到:

foreach ($results as $result) echo "$result <br />";