douzhuoxia0587 2009-07-26 18:27
浏览 22
已采纳

使用XML中的php特定参数查找

Hello i have the follow problem.

I have e XML file and i know the value of "uniquename=value2". I want extract the value of URL.

<set uniquename="value1">
  <items>
    <item>
      <units>
         ...
      </units>
    </item>
    <item>
      <units>
        ...
        <url>http://www.something</url>
        ...
      </units>
    </item>
  </items>
 </set>
 <set uniquename="value2">
  <items>
    <item>
      <units>
         ...
      </units>
    </item>
    <item>
      <units>
        ...
        <url>http://www.something2</url>
        ...
      </units>
    </item>
  </items>
 </set>

Someone has suggestions?

Thanks to all.

  • 写回答

1条回答 默认 最新

  • duanfu7840 2009-07-26 18:33
    关注

    You can do it simply with simplexml and xpath:

    $x = simplexml_load_string($xml);
    $nodes = $x->xpath('set[@uniquename="value2"]/items/item/units/url');
    
    $url = (string)$nodes[0];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?