dream04110 2013-08-01 17:08
浏览 49
已采纳

从simpleXMLElementObject访问数据

I am getting XML data from an api that looks like this:

<?xml version="1.0" encoding="utf-8"?>
<PP>
<row merchant_id="" customer_code="" billing_name="" account_status="Active"  created_date="2013-07-31 13:22:32.687" last_modified_date="2013-07-31 13:23:41.827" last_trans_date="2013-07-31 13:35:59.257" billing_address1="rd." billing_address2="" billing_city="Lower" billing_province_id="" billing_country_id="CA" billing_postal="" billing_email_address="" billing_phone="" velocity_group="" profile_group="" account_ref="12345" card_expiry="0713" cc_notification="" ref1="" ref2="" ref3="" ref4="" ref5="" />
</PP>

I am trying to either access the data in php or turn it into an easily accessible array. I have tried something like this:

$xml = simplexml_load_string($resp);


$fields = array();
foreach ($xml->field as $f) {
    $f = (array) $f->attributes();
    $fields[] = $f['@attributes'];
}

But cannot access the data from that.

When I print that data it shows as this:

SimpleXMLElement Object ( [row] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [merchant_id] => [customer_code] => [billing_name] => John Doe [account_status] => Active [created_date] => 2013-08-01 08:26:31.710 [last_modified_date] => 2013-08-01 08:26:52.170 [last_trans_date] => [billing_address1] => [billing_address2] => [billing_city] => [billing_province_id] => [billing_country_id] => CA [billing_postal] => [billing_email_address] => .ca [billing_phone] => [velocity_group] => [profile_group] => [account_ref] => [card_expiry] => 0813 [cc_notification] => [ref1] => [ref2] => [ref3] => [ref4] => [ref5] => ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [merchant_id] => [customer_code] => 12345 [billing_name] => [account_status] => Active [created_date] => 2013-07-31 13:22:32.687 [last_modified_date] => 2013-07-31 13:23:41.827 [last_trans_date] => 2013-07-31 13:35:59.257 [billing_address1] => rd. [billing_address2] => [billing_city] => Lowe [billing_province_id] => [billing_country_id] => CA [billing_postal] => [billing_email_address] => [billing_phone] => [velocity_group] => [profile_group] => [account_ref] => 12345 [card_expiry] => 0713 [cc_notification] => [ref1] => [ref2] => [ref3] => [ref4] => [ref5] => ) ) ) )

Can anyone help me out on either just accessing the data or help me put this into an array.

Thanks

  • 写回答

1条回答 默认 最新

  • duanmangxie7131 2013-08-01 21:06
    关注

    Access it like this:

    $xml = simplexml_load_string($x)->row; // assume XML in $x
    echo $xml['billing_country_id'];
    

    will output

    CA
    

    that's simple, sin't it.

    see it working: https://eval.in/40171

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?