使用PHP将HTML代码添加到XML文件

使用PHP将HTML代码添加到XML文件,php,xml,Php,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' /> &

我有一个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>]]>
这就是将信息添加到我的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 (Could probably be done better by first placing everything in an array or something)
    $newGame = $scores -> createElement("Game");
    $newGame -> appendChild($scores -> createElement("Header", $_POST['header']));
    $newGame -> appendChild($scores -> createElement("Row", $_POST['row']));
    $newGame -> appendChild($scores -> createElement("Date", $_POST['date']));
    $newGame -> appendChild($scores -> createElement("Time", $_POST['time']));
    $newGame -> appendChild($scores -> createElement("HomeTeam", $_POST['hometeam']));
    $newGame -> appendChild($scores -> createElement("Score", $_POST['score']));
    $newGame -> appendChild($scores -> createElement("AwayTeam", $_POST['awayteam']));
    $newGame -> appendChild($scores -> createElement("Other", $_POST['other']));
    $newGame -> appendChild($scores -> createElement("InfoID", $_POST['infoid']));
    $newGame -> appendChild($scores -> createElement("InfoData", $_POST['infodata']));

    //Add the new <Game> tag under the <Games> tag
    $games -> item(0) -> appendChild($newGame);

    //Save again
    $scores -> save('../scores.xml');

    echo "New game added.";
}
?>

这个php连接到一个如下所示的表单:

<form id="form1" method="post" action="">
<table id="table2">

<tr><td>Header:</td> <td><textarea style=" color:#000;" name="header" cols="73" rows="6" > </textarea></td></tr>

<tr><td>Row:</td> <td><input id='textfield' type="text" size="70" name="row" value='A or B' onfocus="inputFocus(this)" onblur="inputBlur(this)"></td></tr>

<tr><td>Date:</td> <td><input id='textfield' type="text" size="70" name="date" value='Date and time of the match' onfocus="inputFocus(this)" onblur="inputBlur(this)"></td></tr>

<tr><td>Time:</td> <td><input id='textfield' type="text" size="70" name="time" value='Current time' onfocus="inputFocus(this)" onblur="inputBlur(this)"></td></tr>

<tr><td>HomeTeam:</td> <td><input id='textfield' type="text" size="70" name="hometeam" value='Home Team' onfocus="inputFocus(this)" onblur="inputBlur(this)"></td></tr>

<tr><td>Score:</td> <td><input id='textfield' type="text" size="70" name="score" value='Score' onfocus="inputFocus(this)" onblur="inputBlur(this)"></td><td>"3 - 2"</td></tr>

<tr><td>AwayTeam:</td> <td><input id='textfield' type="text" size="70" name="awayteam" value='Away Team' onfocus="inputFocus(this)" onblur="inputBlur(this)"></td></tr>

<tr><td>Other:</td> <td><input id='textfield' type="text" size="70" name="other" value='Additional Info' onfocus="inputFocus(this)" onblur="inputBlur(this)"></td></tr>

<tr><td>InfoID:</td> <td><input id='textfield' type="text" size="70" name="infoid" value='The ID of the Popup' onfocus="inputFocus(this)" onblur="inputBlur(this)" ></td><td></td></tr>

<tr><td>InfoData:</td> <td><textarea id='textfield' value='Data of the Popup' onfocus="inputFocus(this)" onblur="inputBlur(this)" name="infodata" cols="73" rows="6"></textarea></td><td>

<tr><td> </td><td><input type="submit" name="submitted" name="Add new row"></td><td> <td></td> 

</table>

 <style>
 BODY {font-family:"Trebuchet MS", Arial, Helvetica, sans-serif; color:#333; font-size:11px;}
 #textfield {color:#888;}
 #table2 td, tr { padding-right:10px;}
 </style>

标题:
行:
日期:
时间:
主队:
比分:“3-2”
阿瓦泰姆:
其他:
InfoID:
信息数据:
正文{字体系列:“投石机MS”,Arial,Helvetica,无衬线;颜色:#333;字体大小:11px;}
#文本字段{颜色:#888;}
#表2 td,tr{右边填充:10px;}

表单背后的代码可能正在使用以下方式保存表单值:

htmlentities($xml_string);
如果您使用框架来处理表单提交,则可能会传递一个标志(True/False),告知框架在保存数据时不要使用htmlentities。但是,请确保您仍在以某种方式验证字段,以防止注入安全漏洞。(例如,如果存储在数据库中,请使用mysql\u real\u escape\u string()或类似工具。)

你可以试试:

$scores = new DOMDocument();
$scores->substituteEntities = false;
$scores->load('../scores.xml');

我认为您不应该使用CDATA。它有很多问题;通过省略它,我相信一切都会如预期的那样工作


您现在使用的实际上是双重编码(使用CDATA和添加的实体),因此在这种情况下,结果将是错误的。

我只是让它在xml文件中看起来像这样,当您检索它时,您将使用PHP函数
htmlspecialchars\u decode
将它们转换回来

例如:

<?php
$s = "&lt;tr&gt;&lt;td colspan='7' id='headertd'&gt;&#xD;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#xD;
    &lt;img border='0' src='images/euro.png' /&gt;&#xD;
    &nbsp;&nbsp;&nbsp;&nbsp;&#xD;
    UEFA Euro 2012 Qualifications&lt;/td&gt;&lt;/tr&gt;"; 
//The orginal $s is retrieved from the XML file

$s = htmlspecialchars_decode($s);
echo $s; //The orginal code is printed
?>

您可以使用PHP函数CreateCataSection将CDATA添加到xml文件中:

交换

$newGame -> appendChild($scores -> createElement("Header", $_POST['header']));
为了

$h=$scores->createElement(“标题”)//创建一个空标记
$h->appendChild($scores->createCDATA节($\u POST['header'])//向其中添加CDATA元素
$newGame->appendChild($h)//然后将其附加到游戏标签


对于注释,应该是非常直接的。

看起来您正在使用htmlentities函数或类似函数,该函数编码类似的html特殊字符。可能重复,但我使用PHP和XML而不是DB…:/您可以尝试:$scrores->substituteEntities=false;在执行$scores->load('../scores.xml')之前;如果您尝试使用$scores->loadHTMLFile('../scores.xml');在我放在上面的线之后,而不仅仅是一个普通负载();(抓着救命稻草尝试帮助)也没有工作页面出现大量错误无法读取xml:(请原谅我的OCD,但只是为了检查是否正常,您正在进行实例化,然后将替代项设置为false,然后加载…对吗?cname是什么意思?对不起,我不太擅长php,我得到了帮助来编写此代码…您能告诉我应该做什么吗?或者我应该添加什么代码来避免html转换…我有此代码,但我没有id。)ea如何在我的案例中使用抱歉..我是说cdata..摆脱它:)CDATA不是问题。我尝试过在没有CDATA的情况下编写HTML代码,但也遇到了同样的问题。是的。没有CDATA的情况下,这是完全等效的。尽管实体编码时看起来不太好,但这是更好的方法。CDATA不是一个好主意。相信我;巧合的是,我还必须使用xml提要来获取s的足球结果是的,这正是我一直以来的意思:P尽管结果与CDATA解决方案(现在)相同。这个解决方案实际上稍微好一点。有了CDATA,在某些情况下,您仍然可能会使用损坏的XML。感谢您的帮助!我能知道我应该把它放在我贴的代码里的什么地方吗!?:)取决于XML文件内容的输出位置,您有某种类型的用户界面,对吗?thx for ur reply!。。。没有用户界面,我只是通过使用此表单将数据发布到XML文件中来简化工作:)这样它就可以在我的页面上正确发布信息:)这就是我所需要的……太令人沮丧了,我找不到解决方案:(你帮我很好地完成了添加、编辑和删除的所有工作,但我无法解决这个问题!已经2天了,我一直在努力:/i编辑你的帖子,因为我认为我找到了解决方案,希望你接受我的编辑,所以我接受你的答案,但你给了我一个更好的答案:)因此,我重新编辑了您的帖子,并按照您发布的方式进行了修改:我尝试了这一次,也发布了不带CDATA的HTML代码,效果很好:)我认为问题在于删除CDATA,让代码在XML中显示为那样。。它们在主页上看起来是正确的。我仍然不知道您使用XML文件的目的是什么,但是如果您要通过某种类型的软件检索它,这可能是最正确的方法。这就是我使用它的目的:)。。。header标签发布了一个类似Euro Qualifications的标题,如果它是空的,它不会发布标题。我认为它应该是这样的:我很惊讶它能工作,所以它可能是itNp出了问题,只是当我没有相同的服务器设置时,很难找到一些东西。。我想您可以尝试替换
$header=$game->getElementsByTagName(“header”)->item(0)->nodeValue带有
$header=stripslashes($game->getElementsByTagName(“header”)->item(0)->nodeValue)
$newGame -> appendChild($scores -> createElement("Header", $_POST['header']));
$h = $scores -> createElement("Header"); //Create a empty <Header> tag
$h -> appendChild($scores ->createCDATASection ($_POST['header'])); //Add a CDATA element to it
$newGame -> appendChild($h); //Then append it to the game tag