0

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.

BenMorel
  • 31,815
  • 47
  • 169
  • 296
Earth Engine
  • 9,568
  • 4
  • 44
  • 72
  • you have to include that inside `[[CDATA` because you have a `
    `
    – DevZer0 Jul 10 '13 at 23:13
  • 1
    However unusual it is, that is legal XML. `
    ` is a legal XML tag (as long as the DTD or schema allows it), and XML tags can appear within a text node.
    – Earth Engine Jul 10 '13 at 23:22

2 Answers2

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
Community
  • 1
  • 1
hakre
  • 184,866
  • 48
  • 414
  • 792
0

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
DevZer0
  • 13,316
  • 6
  • 25
  • 50