user1950072
user1950072

Reputation: 89

PHP DOM document generating malformed XML feed

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 '

Upvotes: 6

Views: 378

Answers (2)

user1950072
user1950072

Reputation: 89

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.

Upvotes: 1

Ram
Ram

Reputation: 423

I am with @aefxx it would be helpful if you can provide a few lines right before and after the line where you see the malformed opening tag (looks like all examples you showed are missing just one character in opening tag).

Upvotes: 0

Related Questions