1

XML ファイルを作成しようとしていますが、XML ファイルをノードでラップする必要があるため、append を使用するのは簡単ではありません。

これに関するどんな助けも素晴らしいでしょう!!

私の XML は 2 つの異なるノード タイプで構成されています。

<entry id="1_0">
    <title>This is the title</title>
    <description>This is the description...</description>
    <subName>Publishers Name</subName>
    <date>Saturday, June 11th, 2007, 5:46:21 PM</date>
    <BorF>bug</BorF>
</entry>

<vote id="1_0">5</vote>

そして、jQueryを使用してデータ(現在は静的)をPHPファイルに送信する簡単なテストページがあります

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>


<script type="text/javascript" src="jquery-1.3.2.min.js"></script>

<script>

$(function(){


    $("#addVote").click(function() {
        $.ajax({
            type: "POST",
            url: "saveListData.php",
            data: 'url=bugs_features_listdata.xml&listData=\n<vote id="1_2">3</vote>',
            async: false,
            cache: false,
            success: function(data, textStatus) {
                if (window.console) console.log(textStatus);
            },
            complete: function(XMLHttpRequest, textStatus){
                if (window.console) console.log(textStatus);
            }
        });
    });

    $("#addEntry").click(function() {
        $.ajax({
            type: "POST",
            url: "saveListData.php",
            data: 'url=bugs_features_listdata.xml&listData=\n<entry id="1_1">\n\
\t<title>This is the title 1</title>\n\
\t<description>This is the description...</description>\n\
\t<subName>Publishers Name</subName>\n\
\t<date>Saturday, June 11th, 2007, 5:46:21 PM</date>\n\
\t<BorF>bug</BorF>\n\
</entry>',
            async: false,
            cache: false,
            success: function(data, textStatus) {
                if (window.console) console.log(textStatus);
            },
            complete: function(XMLHttpRequest, textStatus){
                if (window.console) console.log(textStatus);
            }
        });
    });


}); 

</script>

</head>

<body>


<a id="addVote" href="#">ADD VOTE</a><br /><br /><a id="addEntry" href="#">ADD ENTRY</a>


</body>
</html>

...現在、それを XML ファイルに追加していますが、開始ノードと終了ノードが必要です。

4

3 に答える 3

4

txwinger は、あなたの質問に対するコメントで正しい考えを持っていました。PHP に多数組み込まれている XML 操作ライブラリの 1 つを使用してノードを追加し、シリアル化してテキスト ファイルとして保存する必要があります。たとえば、 SimpleXML では次のようになります

$xml = simplexml_load_file('test.xml');
$vote = $xml->addChild('vote', '5');
$vote->addAttribute('id','1_0');
$fp = fopen('test.xml', 'w');
fwrite($fp, $xml->asXML());
fclose($fp);

あなたのタスクにより適した他の XML 操作ライブラリがあります

于 2009-07-10T17:43:29.683 に答える
0

I would say it depends on the structure of you XML document. If the “rest” is predictable, you could cut that part from your file, append the new data and paste the “rest” back:

      ⋮
n-1:     <node>last node</node>
n  :  </root>


      ⋮
n-1:     <node>last node</node>
n  :     <node>new node</node>
n+1:  </root>
于 2009-07-10T17:23:10.130 に答える