XML从simplexml_load_string获取值

时间:2012-03-13 20:30:05

标签: php xml

我有这段代码接受我的xml并将其放入一个数组中,我做了一个print_r并得到了这个......

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 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;
}

2 个答案:

答案 0 :(得分: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'>";

并完成了。享受:)

答案 1 :(得分: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
)