PHP XML解析删除属性值

时间:2014-09-10 19:52:11

标签: php xml-parsing

我在下面有以下代码:

<?php

$xml = '<Divisions xmlns="urn:description7a.services.chrome.com">
         <responseStatus responseCode="Successful" description="Successful"/>
         <division id="1">Acura</division>
         <division id="44">Aston Martin</division>
         <division id="4">Audi</division>
         <division id="45">Bentley</division>
         <division id="5">BMW</division>
         <division id="6">Buick</division>
         <division id="7">Cadillac</division>
         <division id="8">Chevrolet</division>
         <division id="9">Chrysler</division>
         <division id="11">Dodge</division>
         <division id="46">Ferrari</division>
         <division id="59">FIAT</division>
         <division id="13">Ford</division>
         <division id="170">Freightliner</division>
         <division id="15">GMC</division>
         <division id="16">Honda</division>
         <division id="17">Hyundai</division>
         <division id="18">Infiniti</division>
         <division id="19">Isuzu</division>
         <division id="20">Jaguar</division>
         <division id="21">Jeep</division>
         <division id="22">Kia</division>
         <division id="47">Lamborghini</division>
         <division id="23">Land Rover</division>
         <division id="24">Lexus</division>
         <division id="25">Lincoln</division>
         <division id="48">Lotus</division>
         <division id="53">Maserati</division>
         <division id="26">Mazda</division>
         <division id="61">McLaren</division>
         <division id="27">Mercedes-Benz</division>
         <division id="2">MINI</division>
         <division id="29">Mitsubishi</division>
         <division id="30">Nissan</division>
         <division id="34">Porsche</division>
         <division id="57">Ram</division>
         <division id="49">Rolls-Royce</division>
         <division id="52">Scion</division>
         <division id="42">Smart</division>
         <division id="37">Subaru</division>
         <division id="56">Tesla</division>
         <division id="39">Toyota</division>
         <division id="40">Volkswagen</division>
         <division id="41">Volvo</division>
      </Divisions>
';

$parse = simplexml_load_string($xml);

print_r($parse);

问题在于它没有将属性值保留在xml中,如

<division id="1">Acura</division>
<division id="44">Aston Martin</division>

PHP正在将其删除为:

array (
  0 => 'Acura',
  1 => 'Aston Martin'
);

我将此传递给JSON所以我需要它保持数组的格式,但需要索引是分区ID属性的值。这就是它现在正在输出的内容:

SimpleXMLElement Object ( 
    [responseStatus] => SimpleXMLElement Object ( 
        [@attributes] => Array ( 
            [responseCode] => Successful 
            [description] => Successful 
        ) 
    ) 
    [division] => Array ( 
        [0] => Acura 
        [1] => Aston Martin 
        [2] => Audi 
        [3] => Bentley 
        [4] => BMW 
        [5] => Buick 
        [6] => Cadillac 
        [7] => Chevrolet 
        [8] => Chrysler 
        [9] => Dodge 
        [10] => Ferrari 
        [11] => FIAT 
        [12] => Ford 
        [13] => Freightliner 
        [14] => GMC 
        [15] => Honda 
        [16] => Hyundai 
        [17] => Infiniti 
        [18] => Isuzu 
        [19] => Jaguar 
        [20] => Jeep 
        [21] => Kia 
        [22] => Lamborghini 
        [23] => Land Rover 
        [24] => Lexus 
        [25] => Lincoln 
        [26] => Lotus 
        [27] => Maserati 
        [28] => Mazda 
        [29] => McLaren 
        [30] => Mercedes-Benz 
        [31] => MINI 
        [32] => Mitsubishi 
        [33] => Nissan 
        [34] => Porsche 
        [35] => Ram 
        [36] => Rolls-Royce 
        [37] => Scion 
        [38] => Smart 
        [39] => Subaru 
        [40] => Tesla 
        [41] => Toyota 
        [42] => Volkswagen 
        [43] => Volvo 
    ) 
)

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

SimpleXML正在使用你的<division>元素组并从中创建一个数组,并在它认为合适的时候分配索引。

您可以做的是在每个<id></id>元素中嵌套<name></name><division></division>标记,然后foreach在新的SimpleXMLObjects数组中嵌套<divisions> <division> <id>16</id> <name>Honda</name> </divsion> <division> <id>45</id> <name>Bently</name> </division> <division> <id>1</id> <name>Acura</name> </division> </divisions> 以这种方式获取您的数据。

因此,如果您的XML代码如下所示:

$xml = simplexml_load_file("divisions.xml");
$divisions = array();

foreach($xml as $division)
{
    $divisions[(int)$division->id] = (string)$division->name;
}

var_dump($divisions);

//Var dump will produce an array of...

array (size=3)
  16 => string 'Honda' (length=5)
  45 => string 'Bently' (length=6)
  1 => string 'Acura' (length=5)

你可以像这样处理它:

{{1}}

这会将所有索引放在正确的位置。