Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP DOM document generating malformed XML feed

Tags:

dom

php

xml

I am trying to create a feed for Google Merchant using php and DOM document.

The feed contains thousands of items, yet fails to validate because there are a tiny handful of cases(4/5 out of 6000+) where the XML is malformed, for example:

 <g:product_type>Plantsg:product_type>

I am generating in a foreach loop

    //Loop through each plant
    foreach($plantList as $plantItem){

        //begin item element
        $item = $xml->createElement('item');


        //Loop through use key as element name and value as data
        foreach ($plantItem as $key => $value)
        {
            //Decode HTML characters, for example '&' becomes  &amp
            //to comply with http://www.w3.org/TR/xhtml1/#C_12
            $decode = htmlspecialchars_decode($value);
            $decode = trim($decode);

            if(empty($decode))
              continue;

            //Create the element
            $tag = $xml->createElement($key);
            $tag = $item->appendChild($tag);

            //Write the field
            $text = $xml->createTextNode($decode);
            $text = $tag->appendChild($text);

        }
        $item = $channel->appendChild($item);
    }

Here is the xml entire generation code.

Here are the 3 malformed tags:

g:adwords_grouping>18</g:adwords_grouping>

form>10 ltr pot</form>

title>Buy Helleborus x nigercors</title>

The malformed tags pop up in different places when I make adjusments to the code. Usually its either missing '

like image 675
user1950072 Avatar asked Jan 24 '13 10:01

user1950072


1 Answers

Thanks to all those who tried to solve it. The malformed tags were put there by Chrome when it encounters an encoding problem.

See annotated screenshot . In this case it opened the , tag encountered 'ö', then fell over and didn't close the tag.

I am also incorrectly using htmlspecialchars_decode when I should be using just htmlspecialchars as pointed out by hakre, but thats a whole different problem.

like image 84
user1950072 Avatar answered Nov 02 '22 09:11

user1950072