Sandeepan Nath
Sandeepan Nath

Reputation: 10284

Simple_xml_load_string with multi-level nodes not loading a node

I have an XML like this, which has some nodes with macros (e.g. {author}) -

<?xml version="1.0"?>
    <catalog>
        <book id="test">
            <author>{author}</author>
            <title>{title}</title>
            <genre>{genre}</genre>
            <price>{price}</price>
            <publish_date>{publish_date}</publish_date>
            <description>{desciption}</description>
        </book>
    </catalog>

Performing simple_xml_load_string() on the above does not return the catalog node, even though no error is displayed (using libxml_use_internal_errors(true) and iterating through libxml_get_errors()). I also validated the xml at http://xmlbeautifier.com/.

Here is the output of simple_xml_load_string() of the above -

SimpleXMLElement Object
(
    [book] => SimpleXMLElement Object
        (
            [author] => {author}
            [title] => {title}
            [genre] => {genre}
            [price] => {price}
            [publish_date] => {publish_date}
            [description] => {desciption}
        )

)

The catalog node is missing.

Upvotes: 0

Views: 98

Answers (1)

mega6382
mega6382

Reputation: 9396

Each XML document has exactly one single root element. It encloses all the other elements and is therefore the sole parent element to all the other elements. ROOT elements are also called PARENT elements. So it is not missing 1 node this is just how it works. If you want the name of the root element try this:

<?php
$xml = simplexml_load_string($str);
echo $xml->getName();
?>

Upvotes: 2

Related Questions