2012-03-19 4 views
0

내 SimpleXmlObject의 요소에 액세스하려고했습니다. 나는 'applicationID'에 접근 할 필요가 있지만 거기에 도착하는데 문제가있다. 나는 성공적으로 다음 코드에서 SimpleXmlObject을 만든 : 이것은합니다 (XML 문자열 $xml에 가정) 각 애플리케이션 ID의 값을 메아리responseHeader가 포함 된 PHP SimpleXML

<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> 

답변

0

(I은 10 응답 문서의 잘린 9했습니다) :

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