PHP 将错误的代码提交到 XML


PHP Submit Wrong codes to XML

我有一个XML文件(scores.xml(,我使用php代码向其添加新标签。

我有一个名为 Header 的标签,其中包含一些 html 代码

<![CDATA[<tr><td colspan='7' id='headertd'>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<img border='0' src='images/euro.png' />
&nbsp;&nbsp;&nbsp;&nbsp;
UEFA Euro 2012 Qualifications</td></tr>]]>

当我以 PGP 脚本的形式编写此代码并提交除标头标签之外的所有内容都正常进入 XML 文件时......我在 PHP 脚本中收到错误,代码在 XML 标记中如下所示:

&lt;![CDATA[&lt;tr&gt;&lt;td colspan='7' id='headertd'&gt;&#13;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#13;
&lt;img border='0' src='images/euro.png' /&gt;&#13;
&nbsp;&nbsp;&nbsp;&nbsp;&#13;
UEFA Euro 2012 Qualifications&lt;/td&gt;&lt;/tr&gt;]]&gt;

所以这给我的 XML 提供了错误的信息...无论如何我可以解决这个问题吗?并避免这些代码的转换?

那是我的PHP代码:

<?php
if (isset($_POST['submitted'])) {//If the user submitted the form, then add to the XML file
    //Load the scores XML file
    $scores = new DOMDocument();
    $scores -> load('../scores.xml');
    //Get the <Games> tag
    $games = $scores -> getElementsByTagName('Games');
    //Create the new <Game> tag 
    $newGame = $scores -> createElement("Game");
    $newGame -> appendChild($scores -> createElement("Header", $_POST['header']));
    //Add the new <Game> tag under the <Games> tag
    $games -> item(0) -> appendChild($newGame);
    //Save again
    $scores -> save('../scores.xml');
    echo "New game added.";
}
?>
<form id="form1" method="post" action="">
Header: <textarea style=" color:#000;" name="header" cols="73" rows="6" > </textarea>
<br><input type="submit" name="submitted" name="Add new row">
</form>

我没有用户界面,只是使用此脚本使我更容易在我的网站上发布内容!

非常感谢您的帮助!

提前感谢!

[编辑 2]嗯,对不起,答案错误,我在考虑这个问题,但是PHP脚本将其转换为"编码"形式是很正常的,因为当您添加html标签时,xml结构将被破坏,因此必须对其进行编码。因此,稍后当您尝试检索数据时,您必须使用 html_entity_decode(( 对其进行解码才能将其正确导出到浏览器。

[编辑1]如我所见,内容是"编码"的,因此在保存数据之前,请使用html_entity_decode((对其进行解码。这里是"最终"代码:

<?php
if (isset($_POST['submitted'])) {//If the user submitted the form, then add to the XML file
    //Load the scores XML file
    $scores = new DOMDocument();
    $scores -> load('../scores.xml');
    //Get the <Games> tag
    $games = $scores -> getElementsByTagName('Games');
    //Create the new <Game> tag 
    $newGame = $scores -> createElement("Game");
    $newGame -> appendChild($scores -> createElement("Header", html_entity_decode($_POST['header'])));
    //Add the new <Game> tag under the <Games> tag
    $games -> item(0) -> appendChild($newGame);
    //Save again
    $scores -> save('../scores.xml');
    echo "New game added.";
}
?>
<form id="form1" method="post" action="">
Header: <textarea style=" color:#000;" name="header" cols="73" rows="6" > </textarea>
<br><input type="submit" name="submitted" name="Add new row">
</form>
在将

$_POST['header']的内容放入标签<Game>之前,请尝试对html_entity_decode内容使用。