0

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

2 回答 2

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>';
        }
     }
  }
于 2012-05-07T17:04:32.253 回答
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;
        }
    }
}
于 2012-03-19T18:48:34.417 回答