PHP SimpleXML with responseHeader included



我在尝试访问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>

感谢您的帮助!我大约一个月前解决了这个问题(直到现在都忘了我在这里发帖)。虽然我的解决方案有点幼稚,而且是根据我的需求定制的,但我很乐意帮助任何有类似问题的人。以下循环通过并给我每个属性的值:

// $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>';
        }
     }
  }

这将回显每个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;
        }
    }
}

相关内容

最新更新