0

i have some xml that I am trying to parse with php with the following code:

$data = simplexml_load_file($file_path)
foreach ($data as $obj):
   //get author, date, etc
   ...
            // get the paths
            foreach ($obj->paths as $current):
                $kind = $current['kind'];
                $action = $current['action'];
                $path = $current->path;

but I cannot get the kind and action attributes for some reason... the path will work, but not the attributes

the xml looks like this:

<log>

<logentry
   revision="xxxx">
    <author>xyz</author>
    <date>my date</date>
    <paths>
       <path
          kind="file"
          action="M">/myPath/woohoo</path>
       <path.... *more paths*
    ....more logentries

Thanks

2 Answers 2

2

Use $current->attributes() to get them.

Sign up to request clarification or add additional context in comments.

Comments

0

You are making use of the right approach to access the attributes (write it like accessing an array with string-keys representing the name of the attribute), however you're doing it on the wrong element:

$root = simplexml_load_file($path);

foreach ($root->logentry->paths as $current)
{
    $path   = $current->path;
    $kind   = $path['kind'];
    $action = $path['action'];
}

As this example shows, you need to access the attributes on $path and not on $current. That's all. You probably have just over-looked that because the $path variable was already in your question.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.