PHP写入SimpleXML并保存
有了以下代码,我需要做一些事情: XMLPHP写入SimpleXML并保存,php,xml,simplexml,Php,Xml,Simplexml,有了以下代码,我需要做一些事情: XML 像这样使用。用元素和子元素替换元素。这是示例代码 $xml = new DOMDocument(); $xml_item = $xml->createElement("Item"); $xml_name = $xml->createElement("Name"); $xml_category = $xml->createElement("Category"); $xml_album->appendChild( $xml_name
像这样使用。用元素和子元素替换元素。这是示例代码
$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");
你读过这个吗?:那么我应该删除这个问题,不是吗?但是我仍然希望能找到一个解决方案。另外,如何保存到xml?如何保存到xml-
asXML
()如何在
中添加1
:$ToDoList->Data->addChild('Test',1')哦,我明白了。对不起,我太粗鲁了。你能把我的全部代码转换成使用PHP DOM的方法吗?嗯,看起来很不错。什么是$xml\u album
?哦,我明白了。只是一个变量。
<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>
$NewItem = $ToDoList->ToDoList->addChild('Item');
$NewItem->addChild("Name","Test");
$NewItem->addChild("Category","Test");
$ToDoList->asXML("test.xml");
$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");