6

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 '

9
  • Could you show us the 4/5 cases that are malformed? Commented Jan 24, 2013 at 11:00
  • Hmm, tough one. Could you provide more context, i.e. the output around the malformed tags? Commented Jan 24, 2013 at 11:24
  • All I see is a blank canvas. Please check again. Commented Jan 24, 2013 at 11:43
  • Have you tried to save the output of $xml->saveXML() to a file and see whether it contains the malformed tags? I'm pretty sure that PHP's DOM extension isn't the cause. Commented Jan 24, 2013 at 11:47
  • 1
    htmlspecialchars_decode looks misused. Also which component is producing this invalid XML. Is that your own code or is that from an external system? Commented Jan 27, 2013 at 16:43

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

if your problem is effectively resolved, it would be best to accept your answer as the solution, otherwise your question sits in the "unanswered" list forever.
0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.