sabre-io / xml

sabre/xml is an XML library that you may not hate.
http://sabre.io/xml/
BSD 3-Clause "New" or "Revised" License
516 stars 77 forks source link

How to render some element as CDATA? #222

Open komareklukas opened 2 years ago

komareklukas commented 2 years ago

I have feed:

<?xml version="1.0" encoding="UTF-8"?>
<SHOP>
    <SHOPITEM id="392">
        <NAME>Andělika obrovská</NAME>
        <GUID>575c1bb4-710a-11eb-b3db-0cc47a6c9370</GUID>
        <SHORT_DESCRIPTION><![CDATA[<p>Výška sazenice cca 20 cm</p>]]></SHORT_DESCRIPTION>
    </SHOPITEM>
</SHOP>

I need to parse it and then render it.

I would like to ask your advice on how to render "SHORT_DESCRIPTION" as CDATA.

$reader = $service->getReader();
$reader->xml(file_get_contents($feedUrl));
$xml = $reader->parse();

//bdump($xml);

$writer = $service->getWriter();
$writer->openMemory();
$writer->setIndent(true);
$writer->startDocument('1.0', 'UTF-8');
$writer->namespaceMap[''] = '';
$writer->write($xml);

//bdump($writer->outputMemory());

Can you please add the code to my sample?

evert commented 2 years ago

Why do you need it? It's almost always a mistake.

komareklukas commented 2 years ago

Because the "SHORT_DESCRIPTION" element contains HTML code that consists of "<" and ">" characters.

Element must be rendered as "CDATA". It is a mandatory requirement for importing feed in the e-shop platform shoptet.cz.

evert commented 2 years ago

It's much better to map those to &lt; ad &gt;. If your e-shop platform requires it, they use a broken xml parser which would be very surprising!