Json编码或序列化XML [英] Json Encode or Serialize an XML

查看:98
本文介绍了Json编码或序列化XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些xml,这是它的简单版本.

I have some xml, this is a simple version of it.

<xml>
<items>
  <item abc="123">item one</item>
  <item abc="456">item two</item>
</items>
</xml>

在内容上使用SimpleXML

Using SimpleXML on the content,

 $obj = simplexml_load_string( $xml );

我可以使用$obj->xpath( '//items/item' );并访问@attributes.

I can use $obj->xpath( '//items/item' ); and get access to the @attributes.

我需要一个数组结果,因此我尝试了json_decode(json_encode($obj),true)技巧,但这似乎正在删除对@attributes(即abc ="123")的访问.

I need an array result, so I have tried the json_decode(json_encode($obj),true) trick, but that looks to be removing access to the @attributes (ie. abc="123").

还有另一种方法可以访问属性并留下数组吗?

Is there another way of doing this, that provides access to the attributes and leaves me with an array?

推荐答案

您可以使用json_encodejson_decode进行搜索,并且可以添加缺少的内容,因为json_encode -ing遵循一些特定的要求SimpleXMLElement的规则.

You can go the route with json_encode and json_decode and you can add the stuff you're missing because that json_encode-ing follows some specific rules with SimpleXMLElement.

如果您对规则及其详细信息感兴趣,我已经写了两篇有关它的博客文章:

If you're interested into the rules and their details, I have written two blog-posts about it:

  • SimpleXML and JSON Encode in PHP – Part I
  • SimpleXML and JSON Encode in PHP – Part II

对于您来说,也许更有趣的是第三部分,它显示了如何修改json序列化并提供自己的格式(例如,保留属性):

For you perhaps more interesing is the third part which shows how you can modify the json serialization and provide your own format (e.g. to preserve the attributes):

附带了完整的示例,下面是代码摘录:

It ships with a full blown example, here is an excerpt in code:

$xml = '<xml>
<items>
  <item abc="123">item one</item>
  <item abc="456">item two</item>
</items>
</xml>';

$obj = simplexml_load_string($xml, 'JsonXMLElement');

echo $json = json_encode($obj, JSON_PRETTY_PRINT), "\n";

print_r(json_decode($json, TRUE));

JSON的输出和数组如下所示,请注意属性是其中的一部分:

Output of JSON and the array is as following, note that the attributes are part of it:

{
    "items": {
        "item": [
            {
                "@attributes": {
                    "abc": "123"
                },
                "@text": "item one"
            },
            {
                "@attributes": {
                    "abc": "456"
                },
                "@text": "item two"
            }
        ]
    }
}
Array
(
    [items] => Array
        (
            [item] => Array
                (
                    [0] => Array
                        (
                            [@attributes] => Array
                                (
                                    [abc] => 123
                                )

                            [@text] => item one
                        )

                    [1] => Array
                        (
                            [@attributes] => Array
                                (
                                    [abc] => 456
                                )

                            [@text] => item two
                        )

                )

        )

)

这篇关于Json编码或序列化XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆