包含responseHeader的PHP SimpleXML

时间:2012-03-19 18:24:22

标签: php simplexml response-headers

我试图访问SimpleXmlObject中的元素。我只需要访问'applicationID',但我很难到达那里。我已经从以下代码成功创建了一个SimpleXmlObject :(我已截断了10个响应文档中的9个)

<response>
    <lst name='responseHeader'>
      <int name='status'>0</int>
      <lst name='params'>
        <str name='q'>applicationDateAdded:NOW()-1</str>
        <str name='wt'>xml</str>
      </lst>
    </lst>
    <result name='response' numFound='10' start='0'>
      <doc>
        <date name='applicationDateAdd'>2012-02-28T16:00:00Z</date>
        <arr name='applicationDescript'>
          <str>description</str>
          <str>desc</str>
        </arr>
        <bool name='applicationFeatured'>false</bool>
        <str name='applicationId'>APPID-00000000017</str>
        <str name='id'>APPID-00000000017</str>
        <str name='type'>APPLICATION</str>
      </doc>
      <doc>...</doc>
      <doc>...</doc>
      <doc>...</doc>
      <doc>...</doc>
      <doc>...</doc>
      <doc>...</doc>
      <doc>...</doc>
      <doc>...</doc>
      <doc>...</doc>
    </result>
</response>

2 个答案:

答案 0 :(得分:1)

感谢您的帮助!我大约一个月前解决了这个问题(忘了我到目前为止已经发布了)。虽然我的解决方案有点少年并且根据我的需求进行定制,但我很乐意帮助任何有类似问题的人。以下循环并给出了每个属性的值:

// $results is the SimpleXmlElement object at the beginning
$numFound = $results -> attributes() -> numFound;
  echo "Number of Results found: ";
  echo $numFound;
  echo '<br><br>';

  if ($numFound > 0) {
     foreach($results -> children() as $content) {
        echo '<br>------------------------<br>';
        foreach ($content -> children() as $sub) {
      $attName = $sub -> attributes();
      echo $attName[0]." = ";
      $count = 0;
      foreach($sub -> children() as $val) {
        $val = $sub -> str[$count++];
        echo $val;
            echo '<br>';
      }
      echo '<br>';
        }
     }
  }

答案 1 :(得分:0)

这将回显每个applicationId的值(假设XML字符串在$xml中):

$xmlObj = simplexml_load_string($xml);

foreach($xmlObj->result->doc as $doc)
{
    foreach($doc->str as $str)
    {
        if($str->attributes()->name == 'applicationId')
        {
            echo 'applicationId: ' . (string)$str . '<br />';
            break 1;
        }
    }
}