繁体   English   中英

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

[英]Add HTML Codes to XML file using PHP

我有一个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 (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>

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

交换

$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

有了评论,它应该很简单。

我认为您不应该使用CDATA。 有很多问题。 通过省略它,我相信一切都会按预期进行。

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

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

htmlentities($xml_string);

如果您使用框架来处理表单提交,则可能传递一个标志(真/假),以告诉框架在保存数据时不要使用htmlentities。 但是,请确保您仍在以某种方式验证字段,以防止注入安全漏洞。 (例如-如果存储在数据库中,请使用mysql_real_escape_string()或类似名称。)

您可以尝试:

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

我只是让它看起来像xml文件中的样子,当您检索它时,可以使用PHP函数htmlspecialchars_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
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM