You are here:  » Parse CDATA


Submitted by updateshop on Mon, 2010-08-09 13:29 in

My CDATA looks like this:

- <generalText>
- <![CDATA[
Aluminium electrolytic capacitor
SMD 105°
Type: SV
Cap. 0.33 uF
Voltage 50 V
Ripple 3 mA
Case: 4 x 5.4 mm
Tolerance: ± 20 %
Temp.range: -40 to +105°C

How can I parse this so it comes on one line in my csv export? With < /br> after each line.

I know that CDATA is normally not parsed but some parsers do and I hope this one does too ;)

Submitted by support on Mon, 2010-08-09 13:37


CDATA is handled normally by Magic Parser - any parser should return CDATA content, the reason for it is to prevent any characters present that are meaningful within XML to be treated as literal data rather than part of the markup; but in terms of the output from a parser there should be no difference between CDATA and entity encoded non-CDATA content.

Within your myRecordHandler() function, that field should be present in the $record["GENERALTEXT"] variable, so as you output that as part of your CSV, you can use PHP's nl2br() function to convert the new-lines in the data into <br />; e.g.

  function myRecordHandler($record)
    print nl2br($record["GENERALTEXT"]);

If the GENERALTEXT field doesn't appear in your $record, double check that you are using the correct Format String for the repeating element you wish to extract in your call to MagicParser_parse(). If you're not sure of course, if you could email me your code and a copy or link to your XML source I'll check it out for you...


Submitted by support on Mon, 2010-08-09 13:42

Hi again,

Just a follow up regarding the xml to csv script that I sent to you, in that file, look for the following code:

    // now create the CSV row itself by using keys of $firstRecord to access
    // the current $record.
    $row = array();

...and REPLACE that with:

    // now create the CSV row itself by using keys of $firstRecord to access
    // the current $record.
    $record["GENERALTEXT"] = nl2br($record["GENERALTEXT"]);
    $row = array();