我想创建一个XML文件,但XML文件需要被包裹在节点......因此,不便于使用追加。

任何帮助,这将是伟大的!

我的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文件,但我需要的开始/结束节点。

有帮助吗?

解决方案

txwinger在他对你的问题的评论的权利的想法。您应该使用PHP的一个许多内置的XML处理库中添加的节点,然后序列化,并将其保存为文本文件。在的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操作库可能适合你的任务更好

其他提示

我会说这取决于你的XML文档的结构。如果“休息”是可以预见的,你可以从你的文件截取一部分,追加新的数据并粘贴“休息”回:

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


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

如果你正在试图做到这一点的快速和肮脏的方式: 如果你在“r +”打开文件,并设置指向最后一个元素和文件末尾之间的空行,它会写在根级元素结束标记,所以只需添加回你写新的子后元件。记住我一直在写所有4个小时的PHP脚本,所以有可能是一个更好的办法,但是这是我在我的4小时生涯拿出最简单的。

$name = $_POST["name"];
$phone = $_POST["phone"];

$fh = fopen("xml-test-1.xml", "r+");
if (!$fh)
{
    die("Failed to open");
}

fseek($fh, -11, SEEK_END);
fwrite($fh, "<person> \n");
fwrite($fh, "<name>$name</name> \n");
fwrite($fh, "<phone>$phone</phone> \n");
fwrite($fh, "</person> \n");
fwrite($fh, "</people> \n");
fclose($fh);

echo "now look and see if anything was added";
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top