modify my xml file via php form

2019-07-29 08:37发布

问题:

This is my xml file and my php code below. I have put an input type which will search student by firstname. Then information about specific student will appear and another button update will appear.

The issue is that I want to modify the information after that. How can I get the element by tag name so that I can modify the information about the specific student?

<students>
  <student>
    <firstname>John</firstname>
    <lasttname>Snow</lasttname>
    <student_id>160600</student_id>
    <gender>male</gender>
    <dob>23-06-95</dob>
    <email>JohnSnow@gmail.com</email>
    <mobilenumber>57675060</mobilenumber>
    <address>albatros, portlouis</address>
    <cohort>BSE15PT</cohort>
    <programme>Software Engineering</programme>
    <mode>PT</mode>
  </student>
  <student>
    <firstname>Jey</firstname>
    <lastname>Lacroix</lastname>
    <student_id>150501</student_id>
    <gender>M</gender>
    <dob>1990-02-22</dob>
    <email>Jey@hotmail.com</email>
    <mobilenumber>57553536</mobilenumber>
    <address>Curepipe</address>
    <cohort>BSE15AFT</cohort>
    <programme>software engineering</programme>
    <mode>FT</mode>
  </student>
</students>
    <?php
    if(isset($_POST['search']))
{
    $xml=simplexml_load_file("studentInstance.xml") or die("Error: Cannot Create Object");

    //query the document
    $name = $_POST['studentname'];

    //$xml = simplexml_load_string($xml);
    $query = $xml->xpath("/students/student[firstname = '$name']");
    $array=$query;
    //echo "<pre>";
    //rint_r($array);
    //echo "</pre>";

    $count=0;
    $size=count($array);
    //echo $count;
    echo "<center>";
    while($count!=count($array)){
    foreach ($array[$count]->children() as $child) {//stores  values in child
        $getElementTag=$child->getName();//get tag so nom
        echo '<label>'.$getElementTag.'</label>'." ";
        echo '<input type="text" value= " '.$child.' " size="30"></intput>';
        echo "<br>";
        echo "<br>";
    }
        $count++;
    }

    echo '<input type="submit" name="modify" value="Update Record">'.'<br>';



echo "***************************";
echo "</center>";
    }

?>
<!DOCTYPE html>
<html>
    <head>
        <title>Searching</title>

    </head>
    <body>
<center>
    <form method="POST" action="searchtest.php">
        <label>Enter Student Name</label>
         <input type="text" name="studentname" pattern="[A-Z][a-z]+" title="Must start with capital letters!"  required><br>
         <br>
        <input type="submit" name="search" value="search">
        </form>
    </center>


    </body>
</html>

回答1:

Consider a dynamic XSLT (the transformation language used to modify XML documents) where form values are passed to the XSLT script. However, several items must change in current script:

  1. You need <form> tags to initiate the $_POST array and send values to server-side. Add needed action below.
  2. You need to give each <input> a distinct name which you already have with $getElementTag. Consider sanitizing the server side $_POST values.
  3. You need a hidden input field to retain the old firstname in case the user changes this value. This field is important as it is used in XSLT to select the appropriate <student> node to update.

PHP Script

Below script contains only the two if (isset(...) conditionals. Integrate into your full script and be sure echoes do not appear above <html> and <head>. Also, the embedded XSLT string is included. Be sure to have the XSLTProcessor extension (php_xsl.dll or php_xsl.so) enabled in .ini file.

<?php

$xml=simplexml_load_file($cd."/FormInput.xml") or die("Error: Cannot Create Object");

if (isset($_POST['modify'])) {    

    $oldfirstname=($_POST['oldfirstname']);       
    $firstname=($_POST['firstname']);
    $lastname=($_POST['lastname']);
    $student_id=($_POST['student_id']);
    $gender=($_POST['gender']);
    $dob=($_POST['dob']);
    $email=($_POST['email']);
    $mobilenumber=($_POST['mobilenumber']);
    $address=($_POST['address']);
    $cohort=($_POST['cohort']);
    $programme=($_POST['programme']);
    $mode=($_POST['mode']);

    $xslstr = '<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
               <xsl:output version="1.0" encoding="UTF-8" indent="yes" />
               <xsl:strip-space elements="*"/>

                    <xsl:template match="@*|node()">
                      <xsl:copy>
                        <xsl:apply-templates select="@*|node()"/>
                      </xsl:copy>
                    </xsl:template>

                    <xsl:template match="student[firstname=\''.$oldfirstname.'\']">
                        <xsl:copy>
                            <firstname>'.$firstname.'</firstname>
                            <lasttname>'.$lastname.'</lasttname>
                            <student_id>'.$student_id.'</student_id>
                            <gender>'.$gender.'</gender>
                            <dob>'.$dob.'</dob>
                            <email>'.$email.'</email>
                            <mobilenumber>'.$mobilenumber.'</mobilenumber>
                            <address>'.$address.'</address>
                            <cohort>'.$cohort.'</cohort>
                            <programme>'.$programme.'</programme>
                            <mode>'.$mode.'</mode>
                        </xsl:copy>
                    </xsl:template>

                </xsl:transform>';

    $xsl = new DOMDocument;
    $xsl->loadXML($xslstr);

    // Configure the transformer
    $proc = new XSLTProcessor;
    $proc->importStyleSheet($xsl); 

    // Transform XML source
    $newXml = $proc->transformToXML($xml);

    // Save into new file
    file_put_contents($cd."/FormInput_php.xml", $newXml);
}

if(isset($_POST['search'])) {
    //query the document
    $name =  $_POST['studentname'];

    $query = $xml->xpath("/students/student[firstname = '$name']");
    $array=$query;

    $count=0;
    $size=count($array);

    echo "<center>";
    echo '<form id="contactform" name="contactform" method="post">';    
    while($count!=count($array)){
        foreach ($array[$count]->children() as $child) {
            $getElementTag=$child->getName();
            echo '<label>'.$getElementTag.'</label>'." ";
            echo '<input type="text" name="'. $getElementTag .'" value= "'.$child.'" size="30"></intput>';
            echo "<br>";
            echo "<br>";
        }
        $count++;
    }
    echo '<input type="hidden" name="oldfirstname" value="'.$name.'"></input>';
    echo '<input type="submit" name="modify" value="Update Record">'.'<br>';


echo "</form>";
echo "***************************";
echo "</center>";
}

?>

HTML Input

XML Output

(new file does not overwrite existing, see how the Jess StackOverflow replaces the old John Snow)

<?xml version="1.0" encoding="UTF-8"?>
<students>
  <student>
    <firstname>Jess</firstname>
    <lasttname>Stackoverflow</lasttname>
    <student_id>999999</student_id>
    <gender>female</gender>
    <dob>10-05-16</dob>
    <email>JStackoverflow@example.com</email>
    <mobilenumber>7777777</mobilenumber>
    <address>Example, Place</address>
    <cohort>HGJD13D</cohort>
    <programme>Web development</programme>
    <mode>FT</mode>
  </student>
  <student>
    <firstname>Jey</firstname>
    <lastname>Lacroix</lastname>
    <student_id>150501</student_id>
    <gender>M</gender>
    <dob>1990-02-22</dob>
    <email>Jey@hotmail.com</email>
    <mobilenumber>57553536</mobilenumber>
    <address>Curepipe</address>
    <cohort>BSE15AFT</cohort>
    <programme>software engineering</programme>
    <mode>FT</mode>
  </student>
</students>