I have this piece of code that takes my xml and puts it into an array, I did a print_r of it and got this…
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 )
I am trying to echo out the amount, but nothing I try is working, I am new to xml and dont really know how to use it. if anyone can put me in the right direction, I would really appropriate it.
I’ve tried this…
echo $xml['amount'];
oh and here is my code…
$xml = simplexml_load_string($cartArray);
if($xml->nodename){
echo "the node exists";
}
$code = $xml->someNode;
$message = $xml->someOtherNode;
Thanks,
- J
PS – XML CODE
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;
}
It will return
I think, you didnt get name value because the attribute name of item and element of XML was same. so i changed
to
and its done. Enjoy 🙂