<?xml version="1.0"?>
<students>
<student>
<id>1</id>
<name>xyz</name>
</student>
<student>
<id>1</id>
<name>abc</name>
</student>
</students>
form.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>
data.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;
}
?>
insert.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个函数,用于添加子项并将值添加到从insert.php调用iam的子项
答案 0 :(得分:0)
尝试使用simplexml
函数:
$xml = simplexml_load_file('students.xml');
foreach($xml as $student)
{
echo "Id: ".$student->id;
echo "Name: ".$student->name;
}