Getting Cdata Content While Parsing Xml File

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



Leave a reply



Submit