【发布时间】:2016-01-25 21:39:05
【问题描述】:
我被难住了。使用 simplexmlelement 后似乎无法访问 XML 数组。 我已经尝试了各种方法,这个站点充满了示例或如何使用 PHP 中的 simplexmlelement 函数将 xml 解析为数据数组。我什至去 PHP 站点查看该函数并查看它是如何工作的。 我是一名中级 PHP 程序员。我现在可以弄清楚这些事情中的大部分。我做的第一件事就是写一个循环,将数据从 simplexmlelement 打印到屏幕上,不行。
这是我的数组:
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 'test@example.com' (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>test@example.com</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;
我知道这很简单,但我现在非常头疼,只想解决这个问题,以便继续前进。 谢谢!
【问题讨论】:
标签: php mysql arrays xml mysqli