[英]PHP Write into SimpleXML and Save
Having the following code, I need to do something: 具有以下代码,我需要做一些事情:
XML XML
<?xml version="1.0" encoding="UTF-8"?>
<ToDoList>
<Item>
<Name>Complete this xml</Name>
<Category>Programming</Category>
</Item>
<Data>
</Data>
</ToDoList>
PHP PHP
<html>
<head>
<style>
b {
font-size:30px;
}
h1 {
font-size:40px;
}
i {
font-size:18px;
}
span {
font-size:24px;
}
</style>
</head>
<body>
<?php
$ToDoList = simplexml_load_file("list/todo.xml");
$Name = $ToDoList->getName();
echo("<h1>".$Name."</h1>");
foreach($ToDoList->children() as $item){
echo("<b>" . $item->getName() . " : " . $item . "<br></b>");
foreach($item->attributes() as $name => $value){
echo("<i>attr. : " . $name . " = " . $value . "<br></i>");
}
foreach($item->children() as $subitem){
echo("<span>" . $subitem->getName() . " : " . $subitem . "<br></span>");
}
}
?>
</body>
</html>
What I need to do is: 我需要做的是:
To write into xml and then save.
要写入xml,然后保存。
I want to write <Test>1</Test>
in <Data></Data>
. 我想在
<Data></Data>
编写<Test>1</Test>
<Data></Data>
。 After that I want to save it back to list/todo.php
. 之后,我想将其保存回
list/todo.php
。
I've tried reading the questions to write and save the xml, but neither of the methods worked. 我尝试阅读问题以编写和保存xml,但是这两种方法均无效。
What code should I use (in php) to solve this problem? 我应该使用什么代码(在php中)解决此问题?
EDIT--- 编辑 - -
I tried to use this code, but it didn't work: 我尝试使用此代码,但是没有用:
$NewItem = $ToDoList->ToDoList->addChild('Item');
$NewItem->addChild("Name","Test");
$NewItem->addChild("Category","Test");
$ToDoList->asXML("test.xml");
Use like this. 这样使用。 replace element with your element and child.this is sample code.
用您的element和child替换element。这是示例代码。
$xml = new DOMDocument();
$xml_item = $xml->createElement("Item");
$xml_name = $xml->createElement("Name");
$xml_category = $xml->createElement("Category");
$xml_album->appendChild( $xml_name );
$xml_album->appendChild( $xml_category );
$xml->appendChild( $xml_album );
$xml->save("test.xml");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.