tags:

views:

201

answers:

2

I've just recently begun learning XML in the context of PHP and Javascript, and I've encountered a problem.

Here's the XML I'm using:

http://www.dkarndtcpa.net/new_site/faq.xml (I still can't figure out how to show the code in this little box here...)

So the issue is I want to put HTML markup within the XML, and then be able to extract this via PHP and embed the HTML in a different file that's reading the XML with SimpleXML. I'm echoing it with something along the lines of

echo $child->asXML();

However, of course the CDATA tags are still there and it doesn't work. So, my question is, is there either a way to A. embed HTML markup in a different way that is ignored by the XML parser but can be used in an HTML document, or B. a way to strip the CDATA tags from the code?

A: 

The CDATA tags should disappear if you use the nodeValue property instead of asXML().

Something like

echo $child->nodeValue;

should do the job.

Pekka
Unfortunately, it seems that nodeValue returns a SimpleXMLElement object, rather than a string or something useful. Or am I supposed to call another function on nodeValue in order to output it?
Will
@Will does `echo (string)$child->nodeValue` work?
Pekka
Still no luck. It's a regular node object [I think], so I doubt I could typecast it any which way.
Will
@Will awww sorry, I mixed up nodeValue with `textContent`. Does that work any better?
Pekka
That appears to return a SimpleXMLElement object as well, except for some reason whenever I output it. OOP -- found it, just had to search a little harder.
Will
A: 

Just had to reload the string in the XML parser, but excluding CDATA.

echo simplexml_load_string($child->asXML(), null, LIBXML_NOCDATA)

Don't mind me.

Will