0
<?xml version="1.0"?>
 <students>
        <student>
             <id>1</id>
             <name>xyz</name>
             </student>
        <student>
             <id>1</id>
             <name>abc</name>
        </student>
        </students>

表单.html

<form action="insert.php" method="post">
<p>Name</p>
<input name="name" required><br>
<p>ID</p>
<input name="id" required><br>
<input type="submit" value="submit" name="submit">

</form>

数据.php

<?php


function c_element($e_name,$parent)    
{    
global $xml;    
$node=$xml->createElement($e_name);    
$parent->appendChild($node);    
return $node;    

}

function c_value($value,$parent)
{

 global $xml;
 $value=$xml->createTextNode($value);
 $parent->appendChild($value);
 return $value;
 }

 ?>

插入.php

<?php    

require 'data.php';    

if(isset($_POST['id'])&& isset($_POST['name'])){    

$s_id=$_POST['id'];    
$s_name=$_POST['name'];    

$xml=new DOMDocument("1.0");   
$xml->load("mydata.xml");   
$root=$xml->getElementsByTagName("students")->item(0);   
$student=c_element("student",$root);   
$id=c_element("id",$student);    
c_value("$s_id",$id);   

$name=c_element("name",$student);   
c_value("$s_name",$name);   

$xml->formatOutput=true;   
$xml->save("mydata.xml");   
}   


?>   

我希望每个学生的标签中的值都是唯一的,我该如何实现?它应该动态添加唯一值。从 form.html 获取数据,将其发布到 insert.php.i ahve 在 data.php 中创建了 2 个函数,用于添加子项并向子项添加值

4

1 回答 1

0

尝试simplexml功能:

$xml = simplexml_load_file('students.xml');

foreach($xml as $student)
{
    echo "Id: ".$student->id;
    echo "Name: ".$student->name;
}
于 2013-09-16T05:52:56.690 回答