2012-09-17 5 views
0

PHP를 통해 REST XML 파일에서 메소드를 얻고 싶습니다. 나는이 형식으로되어있는 지역 REST 파일이 :REST 호출에서 메소드 가져 오기

SimpleXMLElement Object 
(
    [doc] => SimpleXMLElement Object 
     (
     ) 

    [resources] => SimpleXMLElement Object 
     (
      [@attributes] => Array 
       (
        [base] => https://**url** 
       ) 

      [resource] => Array 
       (
        [0] => SimpleXMLElement Object 
         (
          [@attributes] => Array 
           (
            [path] => xml/{accesskey}/project 
           ) 

          [param] => SimpleXMLElement Object 
           (
            [@attributes] => Array 
             (
              [name] => accesskey 
              [style] => template 
              [type] => xs:string 
             ) 

           ) 

          [method] => SimpleXMLElement Object 
           (
            [@attributes] => Array 
             (
              [id] => getAllProjects 
              [name] => GET 
             ) 

            [response] => SimpleXMLElement Object 
             (
              [representation] => SimpleXMLElement Object 
               (
                [@attributes] => Array 
                 (
                  [mediaType] => application/xml; charset=utf-8 
                 ) 

               ) 

             ) 

           ) 
... and so on 

나는 다음과 같은 코드를 가지고 있지만, 그것은 단지 첫 번째 방법 이름 반환

$file="application.wadl"; 
$xml = simplexml_load_file($file); 

foreach($xml->resources[0]->resource->method->attributes() as $a => $b) { 
    echo $b,"\n"; 
} 

내가 그들 모두를 추출하고 싶습니다를 , 단지 첫 번째 것이 아닙니다. 그렇게하는 방법?

답변

1

하나의 요소의 속성을 반복하는 대신 모든 요소를 ​​동일한 이름으로 반복해야합니다. 때문에 SimpleXML을의 마법이이만큼 간단하다

->resources와 같은 다른 연산자 바로 다음에
foreach($xml->resources->resource->method as $method) { 
    echo $method['id'],"\n"; 
} 

는 SimpleXML을 방금 그 이름을 가진 첫 번째 요소를 원하는 가정합니다. 하지만 반복하면 각 객체가 SimpleXML 객체로 제공됩니다.

편집 : XML의 중첩은 재귀가 필요하다는 것을 의미합니다 ($xml->resources->resource->resource->resource->method 등을 봐야 함).

이와 비슷한 것 (테스트되지 않은 예)? 실제로 비 PHP 코드 래퍼이기 때문에

function get_methods($base_url, $node) 
{ 
    $all_methods = array();  

    // Child resources: build up the path, and recursively fetch all methods 
    foreach ($node->resource as $child_resource) 
    { 
     $child_url = $base_url . '/' . (string)$child_resource['path']; 

     $all_methods = array_merge(
      $all_methods, 
      get_methods($child_url, $child_resource) 
     ); 
    } 

    // Methods in this resource: add to array directly 
    foreach ($node->method as $method) 
    { 
     $method_url = $base_url . '/' .(string)$method['id']; 
     $all_methods[$method_url] = (string)$method['id']; 
    } 

    return $all_methods; 
} 

print_r(get_methods('/', $xml->resources)); 

덧붙여, print_r는 항상 당신에게 SimpleXML을 객체의 최고의 전망을 제공하지 않습니다. 대신 this simplexml_dump() function을 시도하십시오.

+0

첫 번째 ID를 반환합니다. [여기] (http://pastebin.com/0EeEyqTU)는 전체 XML 파일이므로 이전에 게시하지 않았습니다. "getProjectSystems"와 같은 다른 ID가 있습니다. – Kokesh

+0

@Kokesh 그'print_r'에서 XML 구조를 읽는 것은 꽤 어렵습니다. 실제 XML 샘플을 게시 할 수 있습니까? 기억해야 할 중요한 점은 위의 코드가 * first *'resource'의 모든 메소드 노드를 반복한다는 것입니다. 그것은 여러개의'resource' 노드를 반복하지 않거나 XML의 다른 곳에서'method' 노드를 재귀 적으로 찾습니다. – IMSoP

+0

죄송합니다. 지금 당장 XML을 업로드하는 것에 대해 생각해 봤습니다. [here is] (http://pastebin.com/N4nSQXy8) – Kokesh

관련 문제