如何将XML文件转换为数组?

时间:2015-03-05 03:20:47

标签: php arrays xml

我试图转换我从网站收到的XML字符串:

   <rosters>
      <roster id_provider="928" id_course="427213" provider_course_code=" " id_roster="0" id_publishing="0" TimeStamp="3/4/2015 9:51:02 PM" provider_publishing_key="8257QP01 ">
        <attendees>
            <attendee id_roster_attendee="" ErrorCode="44" message="Invalid entry for id_license." TimeStamp="3/4/2015 9:51:02 PM" licensee_profession="RN" licensee_number="NONE" licensee_name=" " />
        </attendees>
      </roster>
   </rosters>

我只得到以下内容(加上上面提到的字符串的XML转储):

数组内容如下:SimpleXMLElement Object([0] =&gt;)array(1){[0] =&gt; string(413)&#34;&#34;数组内容结束了!

请帮助!!!

汉克

使用我在其他地方找到的以下代码进行数组。

   echo "this is my xml returned from webservice <p>" . $xmlresponse . "</p>";
        $xml_string = simplexml_load_string($xmlresponse); 
        print_r ("this is my xml after going through simplexml_load_string" . $xml_string);

        echo "<p>finally, I go through json encode and decode. </p>";

        $json = json_encode($xml_string);
        $array = json_decode($json,TRUE);
        echo "<p>Final array contents are as follows</p>";
        print_r($array);
        echo "<p>Array contents ended!</p>";

这是查看代码输出的链接

http://nurse411.com/courses/xml/untitled.php

1 个答案:

答案 0 :(得分:0)

<?php

$xml = '<rosters><roster id_provider="928" id_course="427213" provider_course_code=" " id_roster="0" id_publishing="0" TimeStamp="3/4/2015 9:51:02 PM" provider_publishing_key="8257QP01 "><attendees><attendee id_roster_attendee="" ErrorCode="44" message="Invalid entry for id_license." TimeStamp="3/4/2015 9:51:02 PM" licensee_profession="RN" licensee_number="NONE" licensee_name=" " /></attendees></roster></rosters>';
$array = new SimpleXMLElement($xml);

print_r($array);

?>

输出:

  SimpleXMLElement Object
(
    [roster] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [id_provider] => 928
                    [id_course] => 427213
                    [provider_course_code] =>  
                    [id_roster] => 0
                    [id_publishing] => 0
                    [TimeStamp] => 3/4/2015 9:51:02 PM
                    [provider_publishing_key] => 8257QP01 
                )

            [attendees] => SimpleXMLElement Object
                (
                    [attendee] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [id_roster_attendee] => 
                                    [ErrorCode] => 44
                                    [message] => Invalid entry for id_license.
                                    [TimeStamp] => 3/4/2015 9:51:02 PM
                                    [licensee_profession] => RN
                                    [licensee_number] => NONE
                                    [licensee_name] =>  
                                )

                        )

                )

        )

)
  

或者您可以尝试使用此代码:

<?php

$xmlstring = '<rosters><roster id_provider="928" id_course="427213" provider_course_code=" " id_roster="0" id_publishing="0" TimeStamp="3/4/2015 9:51:02 PM" provider_publishing_key="8257QP01 "><attendees><attendee id_roster_attendee="" ErrorCode="44" message="Invalid entry for id_license." TimeStamp="3/4/2015 9:51:02 PM" licensee_profession="RN" licensee_number="NONE" licensee_name=" " /></attendees></roster></rosters>';

$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);
$array = json_decode($json,TRUE);

print_r($array);

?>

输出:

Array
(
    [roster] => Array
        (
            [@attributes] => Array
                (
                    [id_provider] => 928
                    [id_course] => 427213
                    [provider_course_code] =>  
                    [id_roster] => 0
                    [id_publishing] => 0
                    [TimeStamp] => 3/4/2015 9:51:02 PM
                    [provider_publishing_key] => 8257QP01 
                )

            [attendees] => Array
                (
                    [attendee] => Array
                        (
                            [@attributes] => Array
                                (
                                    [id_roster_attendee] => 
                                    [ErrorCode] => 44
                                    [message] => Invalid entry for id_license.
                                    [TimeStamp] => 3/4/2015 9:51:02 PM
                                    [licensee_profession] => RN
                                    [licensee_number] => NONE
                                    [licensee_name] =>  
                                )

                        )

                )

        )

)

您可以在以下网址阅读更多内容:

  

http://php.net/manual/en/book.simplexml.php