生成格式错误的 XML 提要的 PHP DOM 文档

标签 php xml dom

我正在尝试使用 php 和 DOM 文档为 Google Merchant 创建一个提要。

提要包含数千个项目,但无法验证,因为在极少数情况下(超过 6000 个中有 4/5)XML 格式不正确,例如:

 <g:product_type>Plantsg:product_type>

我在foreach循环中生成

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

这是 3 个格式错误的标签:

g:adwords_grouping>18</g:adwords_grouping>

form>10 ltr pot</form>

title>Buy Helleborus x nigercors</title>

当我对代码进行调整时,格式错误的标签会在不同的地方弹出。通常它要么丢失'

最佳答案

感谢所有试图解决它的人。 Chrome 在遇到编码问题时将格式错误的标签放在那里。

参见 annotated screenshot .在这种情况下,它打开 , 标签时遇到 'ö',然后倒下并且没有关闭标签。

当我应该只使用 htmlspecialchars 时,我也错误地使用了 htmlspecialchars_decode,正如 hakre 所指出的,但那是一个完全不同的问题。

关于生成格式错误的 XML 提要的 PHP DOM 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14499512/

相关文章:

java - 将远程 Mysql 连接到 Netbeans

PHP curl : CURLOPT_CONNECTTIMEOUT vs CURLOPT_TIMEOUT

PHP 水印 - Zubrag

c# - 不使用 XmlDocument.Loadxml() 函数将 XML 反序列化为 JSON

xml - 关于使用 : svn log --with-all-revprops --xml [URL] 显示属性的不一致行为

JavaScript getElementById 空值错误以及如何避免它

php - 对mysqli_fetch_assoc()进行故障排除

.net - 如何使用 xpath 谓词排除满足条件的节点以外的所有节点?

jquery - 如何从文本区域中提取 DOM 对象?

javascript - 在 html 中,可以在 ITEM 基础上更改语言输入吗?