문제

I'm struggling to generate an XML with products retrieved from an online store in a specific XML format to import into a wholesaler site. It's gotten through this URL:

http://movilterra.com/solostocks/

The way I do it is passing the array of products to this XML template that is sent to the output:

<?php echo '<?xml version="1.0" encoding="UTF-8"?>' ?>
    <SINCRO_DETAILS>
        <COMPANY></COMPANY>
        <COUNTRY>67</COUNTRY>
        <PRODUCTS>
            <?php 
            foreach ($products as $product):
            ?>
                <PRODUCT>
                    <PRODUCT_ID><?php echo $product['id_product'] ?></PRODUCT_ID>
                    <TITLE><?php echo '<![CDATA[' . $product['product_name'] . ']]' ?></TITLE>
                    <DESCRIPTION><?php echo '<![CDATA[' . $product['description_short'] . ']]' ?></DESCRIPTION>
                    <SECTOR><?php echo $product['sector'] ?></SECTOR>
                    <PRICE><?php echo $product['price'] ?></PRICE>
                    <SHIPPING>48</SHIPPING>
                    <MINIMUM><?php echo $product['minimal_quantity'] ?></MINIMUM>
                    <UNIT_TYPE>9</UNIT_TYPE>
                    <PHOTOS>
                        <PHOTO><?php echo $product['imagen'] ?></PHOTO>
                    </PHOTOS>
                    <KEYWORDS>
                        <?php 
                        $keywords = explode(',', $product['meta_keywords']);
                        foreach ($keywords as $keyword):
                            ?>
                            <KEYWORD><?php echo '<![CDATA[' . $keyword . ']]' ?></KEYWORD>
                            <?php 
                        endforeach;
                        ?>
                    </KEYWORDS>
                </PRODUCT>
            <?php 
            endforeach;
            ?>
        </PRODUCTS>
</SINCRO_DETAILS>

Note the CDATA blocks, since they might be possibly the root of the problem. All the strings that are not numeric are being previously filtered through a function which converts them like so:

utf8_encode(preg_replace('/[\[\]\!]|CDATA/i', '', strip_tags($str)))

I used utf8_encode() to prevent errors from tildes and other characters. However I'm still having this error:

error on line 1247 at column 18: Extra content at the end of the document

and everytime I reload the URL, the error line changes. If I inspect the code I see that the first TITLE tag (with a CDATA block) seems to swallow every other content.

Could you please help me get this working?

도움이 되었습니까?

해결책

You should add a > at the end of each CDATA declaration (no only the title element, all of them), like so :

<TITLE><?php echo '<![CDATA[' . $product['product_name'] . ']]>' ?></TITLE>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top