2016-01-25 80 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