问题
I have an XML file like the following:
...
<MyElement>Introduction:<br />
...
</MyElement>
...
Are there any way to return "Introduction:" and the rest after </br>
of that element?
I am using PHP and SimpleXML. Naively use echo $document->MyElement
shows "Introduction:...", which mixed up the Introduction line and the content.
回答1:
SimpleXML does not have the concept of text-nodes as you might know it from DOMDocument (see as well with this comparison table).
So in PHP the option is that you import the <MyElement>
element into DOM and you then do the operation there:
# prints "Introduction:"
echo dom_import_simplexml($myElement)->firstChild->data;
// ^
// `- is DOMText
回答2:
to read your xml string use the following code, change root_element
to match your root element name.
$content = simplexml_load_string($xml_string);
echo $content->root_element->MyElement
来源:https://stackoverflow.com/questions/17582470/simplexml-access-separated-text-nodes