doumi1311 2016-06-03 15:21
浏览 26
已采纳

PHP将Array转换为XML

I have an array like so:

[0] => Array
        (
            [Project] => Array
                (
                    [ExternalProjectID] => 53
                    [ProjectName] => Doon Creek
                    [Location] => Array
                        (
                            [Address] => 123 Fake Street
                            [City] => Toronto
                            [Province] => ON
                            [Latitude] => 43.0000
                            [Longitude] =>  -80.0000
                        )

                    [Website] => http://www.website.com/our-communities.php?newcommunity=53
                    [ContactInformation] => Array
                        (
                            [ContactPhone] => 555-5555
                            [ContactEmail] => email@email.com
                            [SalesOfficeAddress] => 123 Fake Street
                            [SalesOfficeCity] => Toronto
                            [SalesOfficeProvince] => ON
                        )

                )

        )

Now I am trying to covert this to XML exactly how this looks in the array, like Project should be a node with everything inside, Location should also be a node with everything inside the Location array inside that node, along with ContactInformation.

This is what I have:

$xml = new SimpleXMLElement();

$node = $xml->addChild('Projects');

array_to_xml($newArray, $node);

echo $xml->asXML();
die();

function array_to_xml($array, &$xml) {
    foreach($array as $key => $value) {
        if(is_array($value)) {
            if(!is_numeric($key)){
                $subnode = $xml->addChild("$key");
                array_to_xml($value, $xml);
            } else {
                array_to_xml($value, $xml);
            }
        } else {
            $xml->addChild("$key","$value");
        }
    }
}

but Project, Location and ContactInformation return as like so:

<Projects>
      <Project />
      <ExternalProjectID>53</ExternalProjectID>
      <ProjectName>Doon Creek</ProjectName>
      <Location />
      <Address>123 Fake St.</Address>
      <City>Toronto</City>
      <Province>ON</Province>
      <Latitude>43.0000</Latitude>
      <Longitude>-80.000</Longitude>
      <Website>http://www.website.com/our-communities.php?newcommunity=53</Website>
      <ContactInformation />
      <ContactPhone>555-5555</ContactPhone>
      <ContactEmail>email@email.com</ContactEmail>
      <SalesOfficeAddress>123 Fake Street</SalesOfficeAddress>
      <SalesOfficeCity>Toronto</SalesOfficeCity>
      <SalesOfficeProvince>ON</SalesOfficeProvince>
      <Project />
</Projects>

My question is how do I fix my XML Output?

  • 写回答

2条回答

  • dongzhaoshi8497 2016-06-03 20:42
    关注

    Almost had it! You simply had to pass the $subnode, not $xml into the recursive call of function:

    // XML BUILD RECURSIVE FUNCTION
    function array_to_xml($array, &$xml) {        
        foreach($array as $key => $value) {               
            if(is_array($value)) {            
                if(!is_numeric($key)){
                    $subnode = $xml->addChild($key);
                    array_to_xml($value, $subnode);
                } else {
                    array_to_xml($value, $subnode);
                }
            } else {
                $xml->addChild($key, $value);
            }
        }        
    }
    
    // EXAMPLE ARRAY
    $newArray = array(
                     "Project" =>
                      array("ExternalProjectID" => 53,
                            "ProjectName" => "Doon Creek",
                            "Location" => 
                             array ("Address" => "123 Fake Street",
                                    "City" => "Toronto",
                                    "Province" => "ON",
                                    "Latitude" => 43.0000,
                                    "Longitude" => -80.0000),
                            "Website" => 
                            "http://www.website.com/our-communities.php?newcommunity=53",
                            "ContactInformation" => 
                             array("ContactPhone" => "555-5555",
                                   "ContactEmail" => "email@email.com",
                                   "SalesOfficeAddress" => "123 Fake Street",
                                   "SalesOfficeCity" => "Toronto",
                                   "SalesOfficeProvince" => "ON")
                           )
                    );
    
    // CREATING XML OBJECT
    $xml = new SimpleXMLElement('<Projects/>'); 
    array_to_xml($newArray, $xml);
    
    // TO PRETTY PRINT OUTPUT
    $domxml = new DOMDocument('1.0');
    $domxml->preserveWhiteSpace = false;
    $domxml->formatOutput = true;
    $domxml->loadXML($xml->asXML());
    
    echo $domxml->saveXML();
    

    Output

    <?xml version="1.0"?>
    <Projects>
      <Project>
        <ExternalProjectID>53</ExternalProjectID>
        <ProjectName>Doon Creek</ProjectName>
        <Location>
          <Address>123 Fake Street</Address>
          <City>Toronto</City>
          <Province>ON</Province>
          <Latitude>43</Latitude>
          <Longitude>-80</Longitude>
        </Location>
        <Website>http://www.website.com/our-communities.php?newcommunity=53</Website>
        <ContactInformation>
          <ContactPhone>555-5555</ContactPhone>
          <ContactEmail>email@email.com</ContactEmail>
          <SalesOfficeAddress>123 Fake Street</SalesOfficeAddress>
          <SalesOfficeCity>Toronto</SalesOfficeCity>
          <SalesOfficeProvince>ON</SalesOfficeProvince>
        </ContactInformation>
      </Project>
    </Projects>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何修改pca中的feature函数
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法