Getting cdata content while parsing xml file
SimpleXML has a bit of a problem with CDATA, so use:
$xml = simplexml_load_file('xmlfile', 'SimpleXMLElement', LIBXML_NOCDATA);
if(!empty($xml))
{
$nodes = $xml->xpath('//xml/events');
}
print_r( $nodes );
This will give you:
Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[date] => 01-10-2009
[color] => 0x99CC00
[selected] => true
)
[event] => SimpleXMLElement Object
(
[title] => You can use HTML and CSS
[description] => This is the description
)
)
)
Keeping CDATA sections while parsing through XML
If you use lxml, you can specify a parser that keeps CDATA:
import lxml.etree
file_name = r'inputData.xml'
parser = lxml.etree.XMLParser(strip_cdata=False)
tree = lxml.etree.parse(file_name, parser)
root = tree.getroot()
c = lxml.etree.Element("c")
c.text = "3"
root.insert(1, c)
tree.write("outputData.xml")
How to handle the CDATA tag while parsing an XML file in iPad
If you need to extract the string from CDATA, you could use this block in foundCDATA:
NSMutableString *lStr = [[NSMutableString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];
How to parse element tags present in the CDATA section of any tag in XML using java
CDATA sections are text nodes. So the parser is correct reading it as a single string. CDATA sections mean that the parser will not do any decoding of entities. You can read more about it in the specification.
If you want to treat the contents of a CDATA section as an XML document or fragment you need to do this manually - in other words: parse it separately.
XML parsing : Reading CDATA
You should be able to use exactly the same code:
description = haber.Element("description").Value
Or
description = (string) haber.Element("description")
LINQ to XML will take care of reading the text for you.
Related Topics
Using Ajax to Pass Variable to PHP and Retrieve Those Using Ajax Again
How to Refresh a Page When a Database Is Updated
Composer Cannot Download Files
Error 1148 MySQL the Used Command Is Not Allowed with This MySQL Version
How to Obtain Anchor Part of Url After # in PHP
How to Know If a User Has Paid for a Subscription
How to Properly Handle Session and Access Token with Facebook PHP Sdk 3.0
Yii2 Disable Bootstrap Js, Jquery and CSS
Push Notifications from Server to User with PHP/Javascript
Wordpress: Loading Multiple Scripts with Enqueue
Having Two Different Sessions in Same Domain
Decrypting Strings in Python That Were Encrypted with Mcrypt_Rijndael_256 in PHP
How to Add Namespace to an Attribute with PHP's Simplexml
Parse Error: Syntax Error, Unexpected T_Function Line 10
Pushnotification Server Side Implementation
Show Hide Payment Methods Based on Selected Shipping Method in Woocommerce