dongtan8532 2011-08-05 15:36 采纳率: 0%
浏览 41
已采纳

使用循环填充XML

I'm trying to figure out a way to populate an XML object more dynamically without having to constantly change the name of the nodes. I some code similar to this:

EDIT: Changed the data to be inline with actual db structure:

$query = ("Select order_id, created_on, updated_on, status from table orders;");

// Execute query
$result = mysql_query($query, $link) or die("Could not complete database query");

// Populate array
while(($resultArray[] = mysql_fetch_assoc($result)) || array_pop($resultArray)); 

  $doc = new DOMDocument();
  $doc->formatOutput = true; 

  $r = $doc->createElement( "data" );
  $doc->appendChild( $r ); 

  foreach( $resultArray as $record )
  {
  $b = $doc->createElement( "record" ); 

  $record1 = $doc->createElement( "Order" );
  $record1->appendChild(
  $doc->createTextNode( $record['order_id'] )
  );
  $b->appendChild( $record1 ); 

  $record2 = $doc->createElement( "Created" );
  $record2->appendChild(
  $doc->createTextNode( $record['created_on'] )
  );
  $b->appendChild( $record2 ); 

  $record3 = $doc->createElement( "Updated" );
  $record3->appendChild(
  $doc->createTextNode( $record['updated_on'] )
  );
  $b->appendChild( $record3 );

  $record4 = $doc->createElement( "Status" );
  $record4->appendChild(
  $doc->createTextNode( $record['status'] )
  );
  $b->appendChild( $record4 );

  $r->appendChild( $b );
  } 

  echo $doc->saveXML();

// Close connection
mysql_close($link);

This is fine, but if I want to add a 3rd column in the query I have to also add it to the loop. I'm sure there is a better way to actually do this.

Any advice?

Thanks.

  • 写回答

3条回答 默认 最新

  • dsnpjz6907 2011-08-05 16:15
    关注

    Maybe this class I wrote will help you out...

    <?PHP
    class ArrayTo
    {
        protected static function array2xml($xml, $arr, $elements)
        {
            foreach($arr as $key => $value)
            {
                $element = isset($elements[0]) ? $elements[0] : $key;
                if(is_array($value))
                {
                    $xml->startElement($element);
                    self::array2xml($xml, $value, array_slice($elements, 1));
                    $xml->endElement();
                    continue;
                }
                $xml->writeElement($element, $value);
                $elements = array_slice($elements, 1);
            }
        }
    
        public static function xml($arr, $root = 'root', $elements = Array(), $version = '1.0', $encoding = 'UTF-8')
        {
            $xml = new XMLWriter();
            $xml->openMemory();
            $xml->startDocument($version, $encoding);
            $xml->startElement($root);
            self::array2xml($xml, $arr, $elements);
            $xml->endElement();
            return $xml->outputMemory(true);
        }
    }
    ?>
    

    How to use it...

    $data = array(
        "column1" => "im column 1",
        "column2" => "im column 2",
        "column3" => "im column 3",
        "column4" => "im column 4",
        "column5" => "im column 5",
    );
    
    echo ArrayTo::xml($data, 'table');
    
    // output
    <?xml version="1.0" encoding="UTF-8"?>
    <table>
        <column1>im column 1</column1>
        <column2>im column 2</column2>
        <column3>im column 3</column3>
        <column4>im column 4</column4>
        <column5>im column 5</column5>
    </table>
    

    You can also use the third argument $elements to override the $key at the level with a custom element. For exmple...

    echo ArrayTo::xml($data, 'table', array('COLUMN_1', 'COLUMN_2'));
    
    // output
    <?xml version="1.0" encoding="UTF-8"?>
    <table>
        <COLUMN_1>im column 1</COLUMN_1>
        <COLUMN_2>im column 2</COLUMN_2>
        <column3>im column 3</column3>
        <column4>im column 4</column4>
        <column5>im column 5</column5>
    </table>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)