Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8538729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:11:29+00:00 2026-06-11T11:11:29+00:00

I would like to get methods from REST XML file via PHP. I have

  • 0

I would like to get methods from REST XML file via PHP.
I have local REST file, which is in this format:

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

I have the following code, but it returns just the first method name:

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

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

I would like to extract all of them, not just the first one. How to do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:11:30+00:00Added an answer on June 11, 2026 at 11:11 am

    Rather than looping over the attributes of one element, you need to loop over all the elements with the same name. Due to the magic of SimpleXML, this is as simple as this:

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

    When followed immediately by another operator, as with ->resources, SimpleXML assumes you just want the first element with that name. But if you loop over, it will give you each of them, as a SimpleXML object.

    EDIT : It looks like the nesting of your XML means you need some form of recursion (you need to look at $xml->resources->resource->resource->resource->method etc).

    Something like this perhaps (untested example)?

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

    Incidentally, print_r won’t always give you the best view of a SimpleXML object, because they are actually wrappers around non-PHP code. Try this simplexml_dump() function instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to get a help from you all. The below is the
I have a Rest service and i would like to consume it using jquery
I have a Sinatra based REST service app and I would like to call
I have a WCF service with several methods. I would like to log the
I would like to get numbers of comments for each posts using mysql. I
I would like to get to know how to redirect output of some program
I would like to get View instance that is used to display specific Preference
I would like to get the generic information ( Counter class) of my CounterPersistence
I would like to get rid of the close/X button on the top right
I would like to get started developing games with Flixel, but im on a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.