SimpleXML hasn't "eaten" the nodes, it just isn't very good at showing them in debug outputs like print_r. The SimpleXML API is optimised for applications of XML as a tree of data, rather than text with markup like you have here.
In a more complex API such as the DOM, your first p element would be shown as having three child nodes:
- A text node containing "Line 1"
- A
br element with an attribute class, value HardReturn
- A text node containing "Line 2"
However, SimpleXML doesn't have a separate object for "text nodes", instead it has an accessor for the combined text content of an element, when you cast to string. In this case, that combines the two text nodes, giving "Line 1Line 2". You can still access the br element, you just can't see where it fits relative to the text.
echo "String content: ", (string)$xml->element->p[0], "\n";
echo "Child element attribute: ", (string)$xml->element->p[0]->br['class'], "\n";
String content: Line 1Line 2
Child element attribute: HardReturn
With the second p element, there's a whole tree of nodes to represent:
- A text node contained "This is "
- A child element
b
- A text node inside the
b element with the text "bold"
- A text node (in the
p element) with the text " text"
Since the string representation only captures directly contained text nodes, the combined text content of the p element is just "This is " + " text".
echo "String content: ", (string)$xml->element->p[1], "\n";
echo "Child element string content: ", (string)$xml->element->p[1]->b, "\n";
String content: This is text
Child element string content: bold
In both cases, we can see that all the nodes are still there, and get out the original XML:
echo "First paragraph: ", $xml->element->p[0]->asXML(), "\n";
echo "Second paragraph: ", $xml->element->p[1]->asXML(), "\n";
First paragraph: <p>Line 1<br class="HardReturn"/>Line 2</p>
Second paragraph: <p>This is <b>bold</b> text</p>
To work directly with the text nodes, you need to use a different API, such as DOM; or, since this example appears to actually be HTML, look for an HTML-oriented library.