Load xml, clean it, save to new xml file on server.

Load xml, clean it, save to new xml file on server.

I am relatively new to programming, so please be kind and detailed.
My entire point of this prototype page/code is to load an xml file, look for a target parts of an addresses, remove that xml node at an ancestors level and save the file back the server with a different name xml.

I have everything working except it seems I can only post the xml .text() but I really want the full xml (with tags) to go to the php page to write the xml file on the server.
The code is currently  bloated with check points, but in the end I don't care about any on screen displays or alerts.
Can anyone tell me how to save my xml object as a file? have I just not found the correct api call? I am open to more elegant or efficient ways.
If I remove the .text() from $clean_xml = $(xml).text();  my alert gets an object but the $.post doesn't perform.

Thoughts?

  1. <html>                                                                  
  2. <head>
  3.         <title>Cleaner</title>                                                                 
  4. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js" type="text/javascript"></script>          
  5. <script type="text/javascript">                                         
  6.             $(document).ready(function() {
  7.                 $("#do_something").click(function () {
  8.                     $.ajax({
  9.                         type: "GET",
  10.                         url: "test.xml",
  11.                         dataType: "xml",
  12.                         success: parseXml,
  13.                         error: errorAlert
  14.                      });
  15.                 });
  16.             });
  17.             function parseXml(xml) {
  18.            $("#output").append($(xml).find("customers").length + " start count<br />");

  19.            $(xml).find("unFormattedAddress").each(function () {
  20.                    $a = $(this).text()
  21.                    if ($a.indexOf("1953") != -1) {
  22.                        $("#output").append($(this).text() + " --- my_target ---<br />");
  23.                        $(this).parent().parent().parent().remove();
  24.                    }
  25.                $("#output").append($(this).text() + " ++Pre text<br />");
  26.            });
  27.            $("#output").append("<hr>");

  28.                 $("#output").append($(xml).find("customers").length + " new count<br />");
  29.                 $(xml).find("unFormattedAddress").each(function () {
  30.                     $("#output").append($(this).text() + " ==Post text<br />");
  31.                 });
  32.                 $("#output").append("<hr><hr>");

  33.                 $clean_xml = $(xml).text();
  34.                 alert ($clean_xml);
  35.                 $.post("save_xml.php", { xml: $clean_xml }, function (data) { alert("Data Loaded: " + data); });

  36.                 $("#box_header").append("---- cleaned file contents  ---- <br />");
  37.                 $('#box').load("file.xml");
  38.             };
  39.             function errorAlert(e, jqxhr) {
  40.                 alert("Something went wrong. --> " + jqxhr);
  41.             };
  42.             function complete_show(xml) {
  43.                 alert(xhr.responseText);
  44.             };
  45. </script>                                                               
  46. </head>                                                                 
  47. <body>
  48. <br /><br /><br />
  49.     <button id="do_something" type="button">GO</button>
  50.          <br /><br /><br />
  51.          <div id="output"></div>
  52.          <div id="box_header"></div>
  53.          <div id="box"></div>
  54. </body>                                                                 
  55. </html>

  1. <?php
  2. $xml = $_POST['xml'];
  3. $file = fopen("file.xml","w");
  4. fwrite($file, $xml);
  5. fclose($file);
  6. echo "ok";
  7. ?>