-1

新しいタグを追加するためにphpコードを使用するxmlファイル(scores.xml)があります...

HTMLコードを含むHeaderというタグがあります

<![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にwong情報を取得しています... とにかくこれを修正できますか?これらのコードの変換を回避しますか?

それは私の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>
4

4 に答える 4

2

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

コメントがあれば、それは非常に簡単です。

于 2012-05-20T20:44:45.120 に答える
1

CDATA を使用するべきではないと思います。それにはたくさんの問題があります。それを除外することで、すべてが期待どおりに機能すると信じています。

あなたが今持っているのは、(CDATA と追加されたエンティティを使用して) 実質的に二重エンコードであるため、この場合、結果は間違ったものになります。

于 2012-05-19T09:14:07.603 に答える
0

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
?>
于 2012-05-19T21:16:51.770 に答える
0

フォームの背後にあるコードは、おそらく次のようなものを使用してフォームの値を保存しています:

htmlentities($xml_string);

フレームワークを使用してフォーム送信を処理している場合、データを保存するときに htmlentities を使用しないようにフレームワークに指示するフラグ (True/False) が存在する可能性があります。ただし、インジェクションのセキュリティ ホールを防ぐために、何らかの方法でフィールドを検証していることを確認してください。(例 - DB に格納する場合は mysql_real_escape_string() などを使用します。)

あなたは試すことができます:

$scores = new DOMDocument();
$scores->substituteEntities = false;
$scores->load('../scores.xml');
于 2012-05-19T00:01:18.213 に答える