views:

499

answers:

3

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"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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.

A: 

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>
Gumbo
@GumboI think that is exactly what I am trying to do... how would one go about doing this?
Justin Lee
+3  A: 

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

Otterfan
Do I need to "install" any of these libraries as I don't think I have access to do so on our current server.
Justin Lee
Probably now. SimpleXML is included by default in all 5.0 + versions of PHP. XMLWriter (another option) is included by default in all 5.1.2+ versions. Your sysadmin might have turned these off, but I doubt it.
Otterfan
Oops make that "probably not".
Otterfan
Great! phpinfo says I've got 5.2.6, so I'll give it a shot. Thanks!
Justin Lee
Is there syntax to give it (add a child node) a single, entirely filled node as a string for it to add (passed to the PHP function by jQuery), versus passing all the variables and building the node in PHP?
Justin Lee
Works perfectly! Thanks!!!
Justin Lee
A: 

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";
sean