Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP写入SimpleXML并保存_Php_Xml_Simplexml - Fatal编程技术网

PHP写入SimpleXML并保存

PHP写入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


像这样使用。用元素和子元素替换元素。这是示例代码

$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')$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");