我正在使用PHP并使用以下XML响应:
<CompressedInventoryResults xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/rpc/Gateway">
<CompressedVehicles>
<F _0="vin" _1="stock" _2="msrp" _3="type" _4="year" _5="make" _6="model" _7="imagelist" _8="stockimage">
<RS>
<R_ID="9002250">
<VS>
<V _0="WA1CMAFP3FA096506" _1="A096506" _2="54305" _3="New" _4="2015" _5="Audi" _6="Q5" _7="http://content.homenetiol.com/640x480/53ef04aa3c46463aaa4fd91b13c00037.jpg|http://content.homenetiol.com/640x480/641d6d0da75d457f9026ba35395afdb2.jpg|http://content.homenetiol.com/640x480/4c42f6ff7c7f44eca4613376afef4b22.jpg|http://content.homenetiol.com/640x480/80327447f3bf4421933ca3cae73f9906.jpg|http://content.homenetiol.com/640x480/df7668c77efc4f9692d7809fcb563c98.jpg|http://content.homenetiol.com/640x480/e253195196374c0884c02660fa860b36.jpg|http://content.homenetiol.com/640x480/cae0afb966cf4f1c8430b8807ef761a2.jpg|http://content.homenetiol.com/640x480/b951eee5230c49a583eb0896bb457795.jpg|http://content.homenetiol.com/640x480/a1521a91be614dc19491549fd97fd482.jpg|http://content.homenetiol.com/640x480/563c00e4b19e45f7aba18277541da4c1.jpg|http://content.homenetiol.com/640x480/f47f11edfbf64d5499b842c8b5076376.jpg|http://content.homenetiol.com/640x480/481eb6fcc7b340628b03978e10c2be17.jpg|http://content.homenetiol.com/640x480/dbf461a4a4f349dbbbf9b5bb0b6a0f36.jpg|http://content.homenetiol.com/640x480/3fafb088370f4ee7bec0a3585ef9cec4.jpg|http://content.homenetiol.com/640x480/10908f2cc9da4d418fa752278f635466.jpg|http://content.homenetiol.com/640x480/bf4c4e649b5b4a9db5eb9113ba2d1918.jpg|http://content.homenetiol.com/640x480/7764048b3b5b478a8cf04fb94edad7a8.jpg|http://content.homenetiol.com/640x480/3685da7d1f224386913033e31ee1ebc1.jpg|http://content.homenetiol.com/640x480/f944ff273af044cd91934aabd7651890.jpg|http://content.homenetiol.com/640x480/d8aed10b7d2f471488a1b3f3f6c084d7.jpg|http://content.homenetiol.com/640x480/00d730f575db4db3958a557747358437.jpg|http://content.homenetiol.com/640x480/f104389450e0403f83eb1fb0ffabe3b3.jpg|http://content.homenetiol.com/640x480/df7a41966f0e42859b63738ecaed163e.jpg|http://content.homenetiol.com/640x480/f7d4638bd43d4a119e54decdd0c1d87d.jpg|http://content.homenetiol.com/640x480/29ea28a68bd9426fb85cfc78fb95bdf3.jpg" _8="" />
<V _0="WAU3GAFD2FN030313" _1="A030313" _2="92095" _3="New" _4="2015" _5="Audi" _6="A8 L" _7="http://content.homenetiol.com/640x480/b358a6a4b58044689c272f098a82e9e9.jpg|http://content.homenetiol.com/640x480/96ac0951ee5043d998d6028ea173ddb8.jpg|http://content.homenetiol.com/640x480/b6a57c34c3bc40acadb09dd6538d6c55.jpg|http://content.homenetiol.com/640x480/17fcb06ee98b41f39a7d9b147652887f.jpg|http://content.homenetiol.com/640x480/3714a45c044a4ddca5a3b7a98b91cd34.jpg|http://content.homenetiol.com/640x480/a5dbc31621574055bbfb7533f4238c14.jpg|http://content.homenetiol.com/640x480/84a9c569786d44b38a65014c20a82160.jpg|http://content.homenetiol.com/640x480/8796e699b9e0451684e0d53d129148b1.jpg|http://content.homenetiol.com/640x480/7c4e8882b392420ca7dfb0f9e9507ede.jpg|http://content.homenetiol.com/640x480/f0d31c64de7549aab70f54fac7bd1056.jpg|http://content.homenetiol.com/640x480/0b4ba4aed8ff41b28c99cb59cc6a6258.jpg|http://content.homenetiol.com/640x480/185f86b4054e4eb08c80eb153527f81a.jpg|http://content.homenetiol.com/640x480/44d65bb194eb4272af48753c8a60622f.jpg|http://content.homenetiol.com/640x480/5d9ac515c8dc4921bc59a1fd1183e95c.jpg|http://content.homenetiol.com/640x480/85c95ff5be6f4bebb79f00e91ac59bf9.jpg|http://content.homenetiol.com/640x480/6be0af9acbc9426d9f4ac9fee1074230.jpg|http://content.homenetiol.com/640x480/cca6e17551f9432fae6859460291d2b2.jpg|http://content.homenetiol.com/640x480/d659f6fccdbb4da0a8bb7f59e493720f.jpg|http://content.homenetiol.com/640x480/7a16f44689d54010b35fa18ce6f1f6cf.jpg|http://content.homenetiol.com/640x480/fdb808588f3045aa9ec85436f5126deb.jpg|http://content.homenetiol.com/640x480/ccf49db7883e47abb14a56a25f050303.jpg|http://content.homenetiol.com/640x480/612d74e48c9e4709ae11645c4dd1ea9b.jpg|http://content.homenetiol.com/640x480/5d398a9e322548cc90bec6ee39307142.jpg|http://content.homenetiol.com/640x480/fc0951a8d8d64e889094f75b8a1f36f2.jpg|http://content.homenetiol.com/640x480/490085ec3cab412ca1aee24c5a169381.jpg|http://content.homenetiol.com/640x480/df9f24289d984879a6cc496c7a9887a6.jpg|http://content.homenetiol.com/640x480/98c616aefd80431687b367dc0805d1e4.jpg" _8="" />
</VS>
</R>
</RS>
</F>
</CompressedVehicles>
<ErrorMessage/>
<InventoryResultType>OK</InventoryResultType>
<IsSuccess>true</IsSuccess>
</CompressedInventoryResults>
如何遍历每个压缩车辆,并将每个带有"V"占位符的条目放入一个数组中,其中"F"条目作为 JSON 输出的标头?
或者有更好的方法来与此响应交互?
最终目标是将更有用的输出用于其他源,这些源不能很好地与 XML 交互,但接受 JSON。
提前谢谢。
文件,您必须使用 XML 解析器。在此答案中,您将了解如何使用 SimpleXML 解析器。要以 JSON 格式对数据进行编码,可以使用json_encode
函数。
首先,您必须知道自己的XML文件/字符串的特定结构。在<CompressedInventoryResults>
下,您有以下结构:
<CompressedVehicles>
<F> <!-- attributes legend -->
<RS>
<R>
<VS>
<V /> <!-- vehicle -->
<V /> <!-- vehicle -->
</VS>
</R>
</RS>
</F>
</CompressedVehicles>
然后,在此示例中还有其他系统节点没有用:<ErrorMessage/>...
基本上,使用 SimpleXML 将 XML 转换为 JSON 格式可能是一项非常简单的任务:
$xml = simplexml_load_string( $xmlString );
$json = json_encode( $xml );
通过这种简单的方法,您可以在变量$json
获得如下所示的 JSON 字符串:
{
"CompressedVehicles": {
"F": {
"@attributes": { ... },
"RS": {
"R": {
"@attributes": { "_ID": "9002250" },
"VS": {
"V": [
{
"@attributes": { ... }
},
{
"@attributes": { ... }
}
]
}
}
}
}
},
"ErrorMessage": {},
"InventoryResultType": "OK",
"IsSuccess": "true"
}
粘贴完整的JSON
重新解码它:
$data = json_decode( $json );
您可以使用对象和数组的标准 PHP 语法添加/修改/删除/打印各种元素。
因此,即以下两行:
echo $data->CompressedVehicles->F->{'@attributes'}->_0) . PHP_EOL;
echo $data->CompressedVehicles->F->{'@attributes'}->_1) . PHP_EOL;
将输出:
vin
stock
否则,如果您想访问车辆数据,您可以通过foreach
循环来实现:
foreach( $data->CompressedVehicles->F->RS->R->VS->V as $vehicle )
{
echo $vehicle->{'@attributes'}->_0 . PHP_EOL;
echo $vehicle->{'@attributes'}->_1 . PHP_EOL;
}
输出:
WA1CMAFP3FA096506
A096506
WAU3GAFD2FN030313
A030313
使用 SimpleXML 创建自定义 JSON
更复杂的操作可以是获取所需的 JSON,特别是由于 XML 的奇怪结构,使用编码属性。
加载 XML 字符串后:
$xml = simplexml_load_string( $xmlString );
您可以在 XML 结构中导航。SimpleXML语法与php StdObject语法非常相似:要选择节点级别,您必须使用->nodeName
;要访问同一级别的单个节点,您可以使用数组语法(->nodeName[0]
(;要访问特定的属性节点,您可以使用数组语法 ( ->nodeName[0]['attributeName']
(。
因此,要选择<CompressedVehicles>
节点及其所有子节点,请执行以下操作:
$CompressedVehicles = $xml->CompressedVehicles;
将节点回显为 XML:
echo $CompressedVehicles->F->RS->R->VS->V[0]->asXML();
# ↑ only first vehicle
输出:
<V _0="WA1CMAFP3FA096506" _1="A096506" _2="54305" _3="New" ... />
如前所述,您必须为<F>
节点中列出的属性创建图例:
<F _0="vin" _1="stock" _2="msrp" _3="type" _4="year" _5="make" _6="model" _7="imagelist" _8="stockimage">
为此,您可以填充一个迭代<F>
节点属性的数组:
$attributes = array();
foreach( $CompressedVehicles->F->attributes() as $key => $val )
{
$attributes[$key] = $val->__toString();
}
现在,在$attributes
数组中,您有以下内容:
Array
(
[_0] => vin
[_1] => stock
[_2] => msrp
[_3] => type
[_4] => year
[_5] => make
[_6] => model
[_7] => imagelist
[_8] => stockimage
)
现在是初始化主数组的时刻:
$data = array();
然后,您可以在所有<V>
节点中执行foreach
循环,并使用 $attributes
中的键填充子数组:
foreach( $CompressedVehicles->F->RS->R->VS->V as $vehicle )
{
$line = array();
foreach( $vehicle->attributes() as $key => $val )
{
$line[$attributes[$key]] = $val->__toString();
}
当你在它的时候,为什么不爆炸图像元素?
$line['imagelist'] = explode( '|', $line['imagelist'] );
此时,您可以将子数组添加到主数组:
$data[] = $line;
}
您可以注意将 SIMPLEXMLElement 对象强制转换为字符串的->__toString()
语法。
现在,在你的$data
你有这个:
Array
(
[0] => Array
(
[vin] => WA1CMAFP3FA096506
[stock] => A096506
[msrp] => 54305
[type] => New
[year] => 2015
[make] => Audi
[model] => Q5
[imagelist] => Array ( ... )
[stockimage] =>
)
[1] => Array ( ... )
)
帕斯特宾完整阵列结构
您可以将其编码为 JSON 字符串格式:
$json = json_encode( $data );
$json
包含(美化(:
[
{
"vin": "WA1CMAFP3FA096506",
"stock": "A096506",
"msrp": "54305",
"type": "New",
"year": "2015",
"make": "Audi",
"model": "Q5",
"imagelist": [
"http:'/'/content.homenetiol.com'/640x480'/53ef04aa3c46463aaa4fd91b13c00037.jpg",
...
],
"stockimage": ""
},
{
"vin": "WAU3GAFD2FN030313",
...
}
]
粘贴完整的JSON