Question

I am trying to create an XML file, but the XML file needs to be wrapped in nodes... hence, not easy to use append.

Any help with this would be great!!

My XML consists of 2 different node types:

<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>

And I've a simple testing page using jQuery to send data (currently static) to a PHP file

<!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>

...that currently appends it to my XML file, but I need the beginning/ending nodes.

Was it helpful?

Solution

txwinger had the right idea in his comment on your question. You should use one of PHPs many built-in XML manipulation libraries to add the node, then serialize it and save it as a text file. In SimpleXML, for instance:

$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);

There are other XML manipulation libraries that might suit your task better

OTHER TIPS

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>

If you are trying to do this the quick and dirty way: If you open the file in "r+" and set the pointer to a blank line between the last element and the end of the file, it will write over the root level elements closing tag, so just add it back after you write the new child element. Bear in mind I have been writing php scripts for all of 4 hours, so there is probably a better way, but this is the simplest I've come up with in my 4 hour career.

$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";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top