3

I'd have the following XML structure:

<entry>
    <id im:id="595831580">blabla</id>
</entry>

Now I want to parse the id 595831580.

I tried:

$idAtt = $xml->entry->id;
$id = $idAtt->attributes();
$id2 = $id['im:id'];

But this does not work :(

How can I fix it?

5
  • if the xml is a string, did you simplexml_load_string(); it, to convert to an object? what does var_dump($xml); show? Commented Mar 21, 2013 at 11:30
  • You have to parse XML with an XML parser and create an object out of it to even have a hope of parsing it reliably; show us what you're using Commented Mar 21, 2013 at 11:30
  • You first need to register the namespace im before accessing the attribute. Commented Mar 21, 2013 at 11:34
  • 1
    Checkout this comment. $idAtt->attributes("im",true)->id. Commented Mar 21, 2013 at 11:35
  • That sounds like an answer @Passerby Commented Mar 21, 2013 at 11:41

2 Answers 2

5

Alright.

You can't use namespace in offsetGet method of SimpleXMLElement, but you can in attributes method:

echo $xml->entry->id->attributes("im",TRUE)->id;

Check out this comment for one more demo.

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

Comments

0

Not sure but maybe something like this

$attr = $xml->id[0]->attributes();
echo $attr['im:id'];

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.