Search code examples
phpapisimplexmlchildren

PHP simpleXML – Access child element


I have an xml file which I want to access using PHP:

xml

<entry_list version="1.0">

<entry id="hypocrite">
    <def>
        <dt>:a person who puts on a false appearance of 
            <d_link>virtue</d_link> 
            or religion
        </dt> 
    </def>
</entry>

I want to echo the entire contents of dt, how do I do so? If I use

$def = $entry_list->entry->def->dt;
echo $def;  

then I get

a person who puts on a false appearance of or religion

Or in other words, the entire contents of dt apart from d link.

tl;dr: I want to echo the entire contents of dt, including d link (positioned correctly).


Solution

  • Try adding

    echo $def->asXml();
    

    However it will print out <dt> and </dt> tags.

    You will not notice them in your browser unless you use htmlspecialchars() when echo'ing.