2

I would like to understand how to access a <tag> by its id and not through array position. Example:

<someNameHere id="hello">
 <home>
 </home>
 <home>
 </home>
</someNameHere>
<someNameHere id="hi">
 <home>
 </home>
 <home>
 </home>
</someNameHere>

I don't want to do this:

$myXML->someNameHere[1]->home[0]

I want to go access someNameHere by its ID "hi".

1 Answer 1

1

You'll have to use XPath for that.

$nodes = $myXML->xpath('//*[@id="hi"]');

if (!empty($nodes))
{
    $someNameHere = $nodes[0];
}
1
  • Thanks! Can you show one example, on adding a child to it ? Or is simple a mether of doing $someNameHere->addchild(yeah) ?
    – punkbit
    Mar 2, 2010 at 20:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.