2016-01-25 105 views
0

我很難過。在使用simplexmlelement後似乎無法訪問XML數組。 我已經嘗試過所有的方法,並且這個站點充滿了示例或者如何使用PHP中的simplexmlelement函數將xml解析爲數據數組。我甚至去了PHP網站來查看功能,看看它是如何工作的。 我是一名中級PHP程序員。我現在可以找出大部分這些東西。我做的第一件事是編寫一個循環來從simplexmlelement將數據打印到屏幕上,不行。PHP XML到mysql數據庫

這裏是我的數組:

object(SimpleXMLElement)[4] 
public 'result' => string '1' (length=1) 
public 'result-text' => string 'OK' (length=2) 
public 'action-type' => string 'add-customer' (length=12) 
public 'result-code' => string '100' (length=3) 
public 'amount' => string '0.00' (length=4) 
public 'customer-id' => string '675700633' (length=9) 
public 'customer-vault-id' => string '675700633' (length=9) 
public 'merchant-defined-field-1' => string 'Red' (length=3) 
public 'merchant-defined-field-2' => string 'Medium' (length=6) 
public 'billing' => 
object(SimpleXMLElement)[5] 
    public 'billing-id' => string '2037042803' (length=10) 
    public 'first-name' => string 'John' (length=4) 
    public 'last-name' => string 'Smith' (length=5) 
    public 'address1' => string '1234 Main St.' (length=13) 
    public 'city' => string 'Beverly Hills' (length=13) 
    public 'state' => string 'CA' (length=2) 
    public 'postal' => string '90210' (length=5) 
    public 'country' => string 'US' (length=2) 
    public 'phone' => string '555-555-5555' (length=12) 
    public 'email' => string '[email protected]' (length=16) 
    public 'cc-number' => string '411111******1111' (length=16) 
    public 'cc-exp' => string '1014' (length=4) 
    public 'priority' => string '1' (length=1) 
public 'shipping' => 
object(SimpleXMLElement)[6] 
    public 'shipping-id' => string '416154095' (length=9) 
    public 'priority' => string '1' (length=1) 

我來到這個數組的一切,我知道該怎麼做。我知道比嘗試編寫完整的代碼更好。所以我測試了這樣訪問數組。

$xml = new SimpleXMLElement($data); 
    echo $xml->result . "<br>"; 
    echo $xml->response['result-text'] . "<br>"; 
    echo $xml->action-type . "<br>"; 
    echo $xml->amount . "<br>"; 

這沒什麼關係。 $ data變量包含xml。

 $xml = <<<XML 
    <?xml version="1.0" encoding="UTF-8"?> 
    <response> 
     <result>1</result> 
     <result-text>OK</result-text> 
     <action-type>add-customer</action-type> 
     <result-code>100</result-code> 
     <amount>0.00</amount> 
     <customer-id>1680673722</customer-id> 
     <customer-vault-id>1680673722</customer-vault-id> 
     <merchant-defined-field-1>Red</merchant-defined-field-1> 
     <merchant-defined-field-2>Medium</merchant-defined-field-2> 
    <billing> 
     <billing-id>268327184</billing-id> 
     <first-name>John</first-name> 
     <last-name>Smith</last-name> 
     <address1>1234 Main St.</address1> 
     <city>Beverly Hills</city> 
     <state>CA</state> 
     <postal>90210</postal> 
     <country>US</country> 
     <phone>555-555-5555</phone> 
     <email>[email protected]</email> 
     <cc-number>411111******1111</cc-number> 
     <cc-exp>1014</cc-exp> 
     <priority>1</priority> 
    </billing> 
    <shipping> 
     <shipping-id>998194109</shipping-id> 
     <priority>1</priority> 
    </shipping> 
    </response> 

    XML; 

我知道這是一些簡單的,我在尋找,但我現在有一個非常大的頭痛,只想讓我可以繼續得到這個了。 謝謝!

回答

1

似乎沒有要在特定你的代碼錯誤什麼,你只需要使用正確的語法來訪問屬性:

$obj = new SimpleXMLElement($xml); 

echo sprintf("result: %s\n", $obj->result); //1 
echo sprintf("result text: %s\n", $obj->{'result-text'}); //OK 
echo sprintf("Billing first name: %s\n", $obj->billing->{'first-name'}); //john