如何使用 PHP 将子项添加到新创建的 XML 节点
How do I add children to a newly created XML node using PHP
我正在尝试使用 simpleXml 的 addChild()
将子节点添加到新创建的节点,但它给了我一个例外:
Warning: SimpleXMLElement::addChild()[simlexmlelement.addchild]: Cannot add child. Parent is not a permanent member of the XML tree in (file address) on line (line number)
而且我得到了很多次,因为我的代码的重要部分是:
<?php
$xml = simplexml_load_file(".db.xml") or die("Sorry, no database file found, we will solve it as soon as possible.");
if($xml->$name->getName() == $name) {
echo "We're sorry, but this account name already exists. Underneath is a table with your signup data anyways. We also sent you an email if you are going to retry later.";
} else {
$xml->db->addChild($name);
$xml->$name->addChild("email", $email);
$xml->$name->addChild("day", $day);
$xml->$name->addChild("month", $month);
$xml->$name->addChild("year", $year);
}
?>
这是我美化后的 XML 代码:
<?xml version="1.0" encoding="UTF-8"?>
<body>
<db>
<example>
<email>example@email.com</email>
<day>01</day>
<month>01</month>
<year>1975</year>
</example>
</db>
</body>
我做错了什么?
注:
我知道变量的值不是问题,因为所有变量在我为页面制作的 table 数据中都显示正确的值。
您应该将 children 添加到引用中,如下所示:
$nametag = $xml->db->addChild($name);
$nametag->addChild("email", $email);
$nametag->addChild("day", $day);
$nametag->addChild("month", $month);
$nametag->addChild("year", $year);
在此处阅读更多内容:
http://php.net/manual/ro/simplexmlelement.addchild.php
示例 nr.1
更新
这里是我用输出测试的代码,所以你可以看到它实际上将标签添加到最终的 xml 结构中:
$xml = simplexml_load_file("db.xml") or die("Sorry, no database file found, we will solve it as soon as possible.");
$name = "a";
$email = "b";
$day = "c";
$month = "d";
$year = "e";
if($xml->$name->getName() == $name) {
echo "We're sorry, but this account name already exists. Underneath is a table with your signup data anyways. We also sent you an email if you are going to retry later.";
} else {
$tagname = $xml->db->addChild($name);
$tagname->addChild("email", $email);
$tagname->addChild("day", $day);
$tagname->addChild("month", $month);
$tagname->addChild("year", $year);
}
var_dump($xml->asXML());
更新 2
要将 xml 保存到文件末尾添加:
$xml->asXML("db.xml"); // this would overwrite your old file
我正在尝试使用 simpleXml 的 addChild()
将子节点添加到新创建的节点,但它给了我一个例外:
Warning: SimpleXMLElement::addChild()[simlexmlelement.addchild]: Cannot add child. Parent is not a permanent member of the XML tree in (file address) on line (line number)
而且我得到了很多次,因为我的代码的重要部分是:
<?php
$xml = simplexml_load_file(".db.xml") or die("Sorry, no database file found, we will solve it as soon as possible.");
if($xml->$name->getName() == $name) {
echo "We're sorry, but this account name already exists. Underneath is a table with your signup data anyways. We also sent you an email if you are going to retry later.";
} else {
$xml->db->addChild($name);
$xml->$name->addChild("email", $email);
$xml->$name->addChild("day", $day);
$xml->$name->addChild("month", $month);
$xml->$name->addChild("year", $year);
}
?>
这是我美化后的 XML 代码:
<?xml version="1.0" encoding="UTF-8"?>
<body>
<db>
<example>
<email>example@email.com</email>
<day>01</day>
<month>01</month>
<year>1975</year>
</example>
</db>
</body>
我做错了什么?
注:
我知道变量的值不是问题,因为所有变量在我为页面制作的 table 数据中都显示正确的值。
您应该将 children 添加到引用中,如下所示:
$nametag = $xml->db->addChild($name);
$nametag->addChild("email", $email);
$nametag->addChild("day", $day);
$nametag->addChild("month", $month);
$nametag->addChild("year", $year);
在此处阅读更多内容: http://php.net/manual/ro/simplexmlelement.addchild.php
示例 nr.1
更新
这里是我用输出测试的代码,所以你可以看到它实际上将标签添加到最终的 xml 结构中:
$xml = simplexml_load_file("db.xml") or die("Sorry, no database file found, we will solve it as soon as possible.");
$name = "a";
$email = "b";
$day = "c";
$month = "d";
$year = "e";
if($xml->$name->getName() == $name) {
echo "We're sorry, but this account name already exists. Underneath is a table with your signup data anyways. We also sent you an email if you are going to retry later.";
} else {
$tagname = $xml->db->addChild($name);
$tagname->addChild("email", $email);
$tagname->addChild("day", $day);
$tagname->addChild("month", $month);
$tagname->addChild("year", $year);
}
var_dump($xml->asXML());
更新 2
要将 xml 保存到文件末尾添加:
$xml->asXML("db.xml"); // this would overwrite your old file