2012-03-13 59 views
-2

我有這樣的一段代碼,把我的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; 
} 
+0

你試過像echo $ xml-> amount; ? 你在var_dump($ xml)中獲得了什麼? – Milap 2012-03-13 20:32:48

+0

$ xml->金額感謝米拉普:) – user979331 2012-03-13 20:37:09

+0

如果我想獲得所有的名字,我會如何迴應? – user979331 2012-03-13 20:38:36

回答

1
<?php 

$xml = "<?xml version='1.0'?>"; 
    $xml .= "<cart>\n"; 

     $xml .= "<status>SUCCESS</status>"; 
     $xml .= "<items>"; 
      $xml .= "<item ids='10'>"; 
      $xml .= "<id>20</id>"; 
      $xml .= "<name>ball</name>"; 
      $xml .= "<price>$50</price>\n"; 
      $xml .= "<qty>500</qty>\n"; 
      $xml .= "<option>color</option>\n"; 
      $xml .= "</item>\n"; 
      $xml .= "</items>\n"; 
      $xml .= "<amount>555</amount>\n"; 

     $xml .= "</cart>\n"; 
     $xmls = simplexml_load_string($xml); 
     $var = $xmls->items->item; 
     print_r($var->name); 
?> 

它將返回

SimpleXMLElement Object 
(
    [0] => ball 
) 

我想,你沒有得到名稱值,因爲項目和XML元素的屬性名相同了。所以我改變

$xml .= "<item id='10'>"; 

$xml .= "<item ids='10'>"; 

,並對其做。享受:)

+0

真棒,謝謝我真的很合適 – user979331 2012-03-13 21:11:04

+0

@ user1193385 did not get upvote :) – Milap 2012-03-13 21:12:58

0
<?php 
$string = <<<XML 
<?xml version='1.0'?> 
<document> 
<title>PHP</title> 
<from>Milap</from> 
<to>Patel</to> 
<body> 
    I love PHP 
</body> 
</document> 
XML; 

$xml = simplexml_load_string($string); 

print_r($xml); 
?> 

輸出: -

SimpleXMLElement Object 
(
    [title] => PHP 
    [from] => Milap 
    [to] => Patel 
    [body] => I love PHP 
) 

print_r($xml->from); 

將返回

SimpleXMLElement Object 
(
    [0] => Milap 
) 
+0

我試過這個和print_r($ xml-> name);並返回SimpleXMLElement對象() – user979331 2012-03-13 20:47:22

+0

你可以編輯問題並顯示你的XML嗎? – Milap 2012-03-13 20:49:21

+0

editted我的問題 – user979331 2012-03-13 20:50:49