Hello hello,

I have loaded XML file into textarea, after editing, how to save it?

Going with:
$sample = $HTTP_POST_VARS['XML_file_textarea'];
$sample = $dom->saveXML();
$dom->save('XML/sample.xml');

does not work because I lose ane XML tag, I only have saved information between tags.

Is this some validation issue where PHP delibarately strip tags or something else?

Please help.

Thanks...

Here is complete code of page:

<?php
$dom = new DOMDocument;
$dom->load('XML/sample.xml');
$dom->formatOutput = true;
$sample = $dom->saveXML();
?>

<table border=1>
<form name="XML_textarea" action="<?=$_SERVER['PHP_SELF']?>" method="post">
<tr>
<td><textarea rows="30" cols="100" name="XML_file_textarea"><?php print $sample; ?></textarea></td>
</tr>
<tr>
<td align="right"><input type="submit" name="submit" value="Submit"></td>
</tr>
</form>
</table>

<?php
if ($HTTP_POST_VARS["submit"]=="Submit"){
$sample = $HTTP_POST_VARS['XML_file_textarea'];

echo $sample . "<br><br><br>";

$sample = $dom->saveXML();
$dom->save('XML/sample.xml');
echo '<br>Changes saved.<br>';
} //if HTTP_POST_VARS
?>

    At the simplest, you could just write the value of $POST['XML_file_textarea'] (you should use $POST, as $HTTP_POST_VARS is deprecated now) to a file via file_put_contents() if using PHP 5, or else fopen() and fwrite() if using PHP 4.

    If you want to use the DOM_XML functions to manipulate the data or perhaps just to validate it, you would do something like:

    $doc = new DOMDocument();
    $doc->loadXML($_POST['XML_file_textarea']);
    if($doc == false)
    {
       // there was a problem, so raise error here
    }
    else
    {
       $doc->save('XML/sample.xml');
    }
    

      Thanks for reply, that work fine but another problem occur...

      Using

      <?php
      $dom = new DOMDocument;
      $dom->load('XML/sample.xml');
      $sample = $dom->saveXML('XML/sample.xml');
      ?>

      <form name="XML_textarea" action="" method="post">

      <td><textarea rows="30" cols="100" name="XML_file_textarea"><?php print $sample; ?></textarea></td>
      </tr>

      I have error with your code and it probably is because saveXML function.
      For example, XML file like this:
      <Admin_Passwd ua="na"></Admin_Passwd>
      <User_Password ua="rw"></User_Password>

      in text are would be:
      <Admin_Passwd ua="na"/>
      <User_Password ua="rw"/>

      Error is:
      Warning: DOMDocument::loadXML() [function.DOMDocument-loadXML]: String not started expecting ' or " in Entity, line: 1

      In XML file I have this definition:
      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>

        In XML,

        <Admin_Passwd ua="na"></Admin_Passwd>
        <User_Password ua="rw"></User_Password>
        

        means exactly the same thing as

        <Admin_Passwd ua="na"/>
        <User_Password ua="rw"/>
        

        and any XML processor should interpreted both in exactly the same way.

        One problem though is that if that is supposed to be a complete XML document, and not just an excerpt, then it's not well-formed because there is no root element.

        does not work because I lose ane XML tag, I only have saved information between tags.

        Is this some validation issue where PHP delibarately strip tags or something else?

        Depends on how you're viewing the resulting string. If you're trying to embed it in an HTML document (between <textarea> tags, say), then you'd need to [man]htmlspecialchars[/man] it first so that the browser doesn't try to render it as HTML (which would probably not work - for one thing, it would just ignore all the tags it doesn't recognise).

          Hello,

          I just want to thank you for your help and time. At the and, it works with mentioned functions file_get_contents and file_put_contents... + function stipslashes.

          Thanks again... 🙂

            Write a Reply...