2014-04-13 58 views
0

剛開始使用XML,遇到了一些麻煩。我有一個我已經創建的XML文檔。這個xml文檔被用作我網站註冊用戶的數據庫。從XML開始

customer.xml

<?xml version="1.0"?> 
<customers> 
<customer> 
    <firstname>John</firstname> 
    <lastname>Willson</lastname> 
    <email>[email protected]</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", "[email protected]"); 
$password->setAttribute("password", "Pa$$w0rd2"); 
$custid->setAttribute("custid", "2222"); 

$customer->appendChild($firstname); 
$customer->appendChild($lastname); 
$customer->appendChild($email); 
$customer->appendChild($password); 
$customer->appendChild($custid); 

我遵循了「打印$ dom-> saveXML();」但不幸的是,它顯示xml文檔中根本沒有任何變化。它只顯示原始信息。我在XML方面很新,所以對我做錯的任何解釋都會很棒。

乾杯,

+1

你最好使用真正的數據庫。 – Quentin

+0

您在結尾處缺少'' –

+0

XML由節點組成,元素是最明顯但只有一種類型。你有單獨的方法來創建節點(creatElement,createTextNode,...)http://stackoverflow.com/a/21760903/2265374 – ThW

回答

-1

你忘了新節點「客戶」節點追加到(即將成爲)父節點的「客戶」,而你寫一個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 = '[email protected]'; 
$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); 
+0

不要直接設置nodeValue。這是實體處理的一個問題:https://bugs.php.net/bug.php?id=31613 – ThW

-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","[email protected]"); 
$customerXml->addChild("custid","1111"); 
$customerXml->addChild("password","P$$w0rd"); 
$customerXml->asXML($file); 
+1

提醒:SimpleXML會扼殺格式不正確的XML。 – Kei

+0

正確,但如果程序員無法獲得其他工作,則更簡單。 – GiantCowFilms

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不使用屬性。您需要爲您的數據創建更多節點。我會把它作爲讀者的練習;-)