我有這樣的一段代碼,把我的XML,並把它放到一個數組中獲取值,我做了它的print_r和得到這個...XML從simplexml_load_string
SimpleXMLElement Object ([status] => SUCCESS [items] => SimpleXMLElement Object ([item] => Array ([0] => SimpleXMLElement Object ([@attributes] => Array ([id] => 1_) [id] => 1 [name] => Product 3 [price] => 20.00 [qty] => SimpleXMLElement Object () [option] => SimpleXMLElement Object ()) [1] => SimpleXMLElement Object ([@attributes] => Array ([id] => 3_) [id] => 3 [name] => Test Fin [price] => 30.00 [qty] => SimpleXMLElement Object () [option] => SimpleXMLElement Object ()))) [amount] => 50)
我想呼應出的數量,但沒有我嘗試工作,我是新來的XML和真的不知道如何使用它。如果任何人都可以把我放在正確的方向,我真的很適合它。
我已經試過這...
echo $xml['amount'];
哦,這裏是我的代碼...
$xml = simplexml_load_string($cartArray);
if($xml->nodename){
echo "the node exists";
}
$code = $xml->someNode;
$message = $xml->someOtherNode;
感謝,
- Ĵ
PS - XML代碼
public function getCartItems() {
$xml = "<?xml version='1.0'?>\n";
$xml .= "<cart>\n";
if(strlen($_SESSION["items"])==0) {
$xml .= "<status>ERR</status>\n";
$xml .= "<message>Cart is empty</message>\n";
}
else {
$xml .= "<status>SUCCESS</status>\n";
$xml .= "<items>\n";
$total = 0;
$this->itemArr = unserialize($_SESSION["items"]);
foreach ($this->itemArr as $item) {
$xml .= "<item id=\"".$item["id"] . '_' .$item["option"] ."\">\n";
$xml .= "<id>".$item["id"]."</id>\n";
$xml .= "<name>".$item["name"]."</name>\n";
$xml .= "<price>".$item["price"]."</price>\n";
$xml .= "<qty>".$item["qty"]."</qty>\n";
$xml .= "<option>".$item["option"]."</option>\n";
$xml .= "</item>\n";
$total += ($item["price"]);
}
$xml .= "</items>\n";
$xml .= "<amount>".$total."</amount>\n";
}
$xml .= "</cart>\n";
return $xml;
}
你試過像echo $ xml-> amount; ? 你在var_dump($ xml)中獲得了什麼? – Milap 2012-03-13 20:32:48
$ xml->金額感謝米拉普:) – user979331 2012-03-13 20:37:09
如果我想獲得所有的名字,我會如何迴應? – user979331 2012-03-13 20:38:36