刚开始使用XML,遇到一些麻烦。我有一个我已经创建的XML文档。此xml文档正用作我网站注册用户的数据库。
customer.xml
<?xml version="1.0"?>
<customers>
<customer>
<firstname>John</firstname>
<lastname>Willson</lastname>
<email>a@b.c</email>
<custid>1111</custid>
<password>Pa$$w0rd</password>
</customer>
我现在正在创建注册页面,因此我需要能够在现有的xml文档中添加新客户。看了一下后,这就是我在注册功能中提出的:
$xmlFile = "customer.xml";
$dom = DOMDocument::load($xmlFile);
$customer = $dom->createElement( "Customer" );
$firstname = $dom->createElement( "firstname" );
$lastname = $dom->createElement( "lastname" );
$email = $dom->createElement( "email" );
$password = $dom->createElement( "password" );
$custid = $dom->createElement( "custid" );
$firstname->setAttribute( "firstname", "Fred" );
$lastname->setAttribute( "lastname", "Fredson" );
$email->setAttribute( "email", "d@e.f" );
$password->setAttribute( "password", "Pa$$w0rd2" );
$custid->setAttribute( "custid", "2222" );
$customer->appendChild($firstname);
$customer->appendChild($lastname);
$customer->appendChild($email);
$customer->appendChild($password);
$customer->appendChild($custid);
我使用&#34; print $ dom-&gt; saveXML();&#34;但不幸的是,它表明xml文档中根本没有任何变化。它只显示原始信息。我对XML非常陌生,所以对我做错的任何解释都会很棒。
干杯,
答案 0 :(得分:0)
首先,您显然没有看到PHP通知(可能是警告和错误),因为您的代码触发了其中一些:
// Strict standards: Non-static method DOMDocument::load() should not be called statically
$dom = DOMDocument::load($xmlFile);
你想:
$dom = new DOMDocument();
$dom->load($xmlFile);
// Notice: Undefined variable: w0rd2
$password->setAttribute( "password", "Pa$$w0rd2" );
你想:
$password->setAttribute( "password", 'Pa$$w0rd2' ); // Single quotes
您需要先配置完整的错误报告。
修正了其他一些问题。 XML区分大小写:
$customer = $dom->createElement( "Customer" );
你想:
$customer = $dom->createElement( "customer" );
您创建此节点并将其他所有内容附加到该节点,但您永远不会将其插入文档中。你想要:
$dom->getElementsByTagName('customers')->item(0)->appendChild($customer);
此外,正如另一个答案中已经指出的那样,您的示例XML不使用属性。您需要为数据创建其他节点。我将把它作为读者的练习; - )
答案 1 :(得分:-1)
您忘记追加新节点&#34;客户&#34;节点到(futur)父节点&#34; customers&#34;,并且你已经为&#34; Customer&#34;写了一个C大写字母。您将新节点的节点内容设置为属性,但这些是节点值(或子节点,文本节点),而不是属性。
$xmlFile = "customer.xml";
$dom = DOMDocument::load($xmlFile);
$customer = $dom->createElement( "customer" ); // here
$firstname = $dom->createElement( "firstname" );
$lastname = $dom->createElement( "lastname" );
$email = $dom->createElement( "email" );
$password = $dom->createElement( "password" );
$custid = $dom->createElement( "custid" );
$firstname->nodeValue = 'Fred';
$lastname->nodeValue = 'Fredson';
$email->nodeValue = 'd@e.f';
$password->nodeValue = 'Pa$$w0rd2'; // be careful with $ and double quotes
$custid->nodeValue = '2222';
$customer->appendChild($firstname);
$customer->appendChild($lastname);
$customer->appendChild($email);
$customer->appendChild($password);
$customer->appendChild($custid);
$dom->getElementsByTagName('customers')->item(0)->appendChild($customer);
答案 2 :(得分:-1)
简单的XML就是你的答案。 http://www.php.net/manual/en/book.simplexml.php
$customerXml = new SimpleXMLElement($xmlFile);
$customerXml->addChild("firstname","Jhon");
$customerXml->addChild("lastname","Willson");
$customerXml->addChild("email","a@b.c");
$customerXml->addChild("custid","1111");
$customerXml->addChild("password","P$$w0rd");
$customerXml->asXML($file);