0

PHPを介してRESTXMLファイルからメソッドを取得したいと思います。次の形式のローカル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";
}

最初のものだけでなく、それらすべてを抽出したいと思います。どうやってするか?

4

1 に答える 1

1

1つの要素の属性をループするのではなく、同じ名前のすべての要素をループする必要があります。SimpleXMLの魔法により、これは次のように単純です。

foreach($xml->resources->resource->method as $method) {
    echo $method['id'],"\n";
}

SimpleXMLは、のように直後に別の演算子が続く場合->resources、その名前の最初の要素が必要であると想定します。ただし、ループすると、SimpleXMLオブジェクトとしてそれぞれが提供されます。

編集: XMLのネストは、何らかの形の再帰が必要であることを意味しているようです($xml->resources->resource->resource->resource->methodなどを確認する必要があります)。

おそらくこのようなもの(未検証の例)?

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_rSimpleXMLオブジェクトは実際には非PHPコードのラッパーであるため、常に最良のビューが得られるとは限りません。代わりにこのsimplexml_dump()機能を試してください。

于 2012-09-17T12:01:13.967 に答える