【问题标题】:PHP DOM document generating malformed XML feedPHP DOM 文档生成格式错误的 XML 提要
【发布时间】:2013-01-24 10:49:15
【问题描述】:

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

Feed 包含数千个项目,但无法验证,因为有极少数情况(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>

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

【问题讨论】:

  • 您能告诉我们 4/5 格式错误的案例吗?
  • 嗯,艰难的一个。您能否提供更多上下文,即格式错误标签周围的输出?
  • 我看到的只是一张空白画布。请再次检查。
  • 您是否尝试过将$xml-&gt;saveXML() 的输出保存到文件中并查看它是否包含格式错误的标签?我很确定 PHP 的 DOM 扩展不是原因。
  • htmlspecialchars_decode 看起来被滥用了。还有哪个组件产生了这个无效的 XML。这是您自己的代码还是来自外部系统的代码?

标签: php xml dom


【解决方案1】:

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

见。在这种情况下,它打开 , 标签遇到 'ö',然后摔倒并没有关闭标签。

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

【讨论】:

  • 如果您的问题得到有效解决,最好接受您的回答作为解决方案,否则您的问题将永远处于“未回答”列表中。
【解决方案2】:

我支持@aefxx,如果您可以在您看到格式错误的开始标记的行之前和之后提供几行,那将会很有帮助(看起来您展示的所有示例在开始标记中都只缺少一个字符)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多