如何为XML创建数字索引数组?

时间:2017-06-30 20:32:18

标签: php arrays xml codeigniter

我正在使用PHP进行数组到XML的转换。我使用以下代码:

function array_to_xml($template_info, &$xml_template_info) {
            foreach($template_info as $key => $value) {
                if(is_array($value)) {
                    if(!is_numeric($key)){

                        $subnode = $xml_template_info->addChild("$key");

                        if(count($value) >1 && is_array($value)){
                            $jump = false;
                            $count = 1;
                            foreach($value as $k => $v) {
                                if(is_array($v)){
                                    if($count++ > 1)
                                        $subnode = $xml_template_info->addChild("$key");

                                    array_to_xml($v, $subnode);
                                    $jump = true;
                                }
                            }
                            if($jump) {
                                goto LE;
                            }
                            array_to_xml($value, $subnode);
                        }
                        else
                            array_to_xml($value, $subnode);
                    }
                    else{
                        array_to_xml($value, $xml_template_info);
                    }
                }
                else {
                    $xml_template_info->addChild("$key","$value");
                }

                LE: ;
            }
        }

但问题是我无法为sub node创建具有numerical array值的数字标准值,

我正在尝试实现以下方法。

Array
(
    [DateTimeStamp] => 06/30/2017 08:11:23
    [Sender] => Array
        (
            [SenderID] => TRN
            [SenderName] => Transportation Reservation System,Inc.
        )

    [Recipient] => Array
        (
            [RecipientID] => DATA1
            [RecipientName] => 
        )
    [Payload] => Array
        (
            [RateProduct] => Array
                (
                    [0] => Array
                        (
                            [RateVendor] => 42074
                            [RateId] => 13
                            [RentalLocationID] => 262
                            [RateCompanyID] => RCR
                            [TotalPricing] => Array
                            (
                                [RentalDays] => 1
                                [RateCharge] => 127.50
                            )
                            [Taxes] => Array
                                    (
                                        [Tax2Amount] => 154.04
                                        [Tax2Rate] => 0.13
                                    )
                            [0] => Array
                                    (
                                        [DailyExtra] => Array
                                            (
                                                [ExtraCode] => ERF
                                                [ExtraDesc] => ENERGY RECOVERY FEES
                                            )

                                    )

                              [1] => Array
                                    (
                                        [DailyExtra] => Array
                                            (
                                                [ExtraCode] => ERF
                                                [ExtraDesc] => ENERGY RECOVERY FEES
                                            )

                                    )
                       )
                    [1] => Array
                        (
                            [RateVendor] => 42074
                            [RateId] => 13
                            [RentalLocationID] => 262
                            [RateCompanyID] => RCR
                            [TotalPricing] => Array
                            (
                                [RentalDays] => 1
                                [RateCharge] => 127.50
                            )
                            [Taxes] => Array
                                    (
                                        [Tax2Amount] => 154.04
                                        [Tax2Rate] => 0.13
                                    )
                            [0] => Array
                                    (
                                        [DailyExtra] => Array
                                            (
                                                [ExtraCode] => ERF
                                                [ExtraDesc] => ENERGY RECOVERY FEES
                                            )

                                    )

                              [1] => Array
                                    (
                                        [DailyExtra] => Array
                                            (
                                                [ExtraCode] => ERF
                                                [ExtraDesc] => ENERGY RECOVERY FEES
                                            )

                                    )
                       )
                 )
          )
)

array值需要更改为以下格式

<?xml version="1.0" encoding="UTF-8"?>
<PRE>
    <TRNXML Version="1.0.0" TimeZone="ET" />
    <DateTimeStamp>06/30/2017 04:36:04</DateTimeStamp>
    <Sender>
        <SenderID>TRN</SenderID>
        <SenderName>Transportation Reservation System,Inc.</SenderName>
    </Sender>
    <Recipient>
        <RecipientID>ROUTES</RecipientID>
        <RecipientName />
    </Recipient>
    <Payload>
        <RateProduct>
            <RateVendor>FCAR</RateVendor>
            <RateID />
            <RentalLocationID>FCAR</RentalLocationID>
            <RateCompanyID>FCAR</RateCompanyID>
            <TotalPricing>
                <RentalDays>FCAR</RentalDays>
                <RateCharge>FCAR</RateCharge>
            </TotalPricing>
            <Taxes>
                <Tax2Amount>FCAR</Tax2Amount>
                <Tax2Rate>FCAR</Tax2Rate>
            </Taxes>
            ///this has to come in following format
            <DailyExtra>
                <ExtraCode>FCAR</ExtraCode>
                <ExtraDesc>FCAR</ExtraDesc>
            </DailyExtra>
            <DailyExtra>
                <ExtraCode>FCAR</ExtraCode>
                <ExtraDesc>FCAR</ExtraDesc>
            </DailyExtra>
        </RateProduct>
    </Payload>
</PRE>

如何使其在XML代码中运行?

2 个答案:

答案 0 :(得分:1)

检查一下:     

#declare
/**
 * $array - IN array
 * $dom - DOMDocument object
 * $node - DomElement
 * 
 * */
function array_to_xml($array, &$dom, &$node){
    foreach($array as $key=>$value){
        if( is_array( $value ) ){
            if ( is_numeric($key) ){
                array_to_xml($value, $dom, $node);
            }
            elseif( is_string($key) ){
                $el = $dom->createElement($key);
                array_to_xml($value, $dom, $el);
                $node->appendChild($el);
            }
        }
        elseif ( empty($value) ){
            $el = $dom->createElement($key);
            $node->appendChild($el);
        }
        elseif( is_string($key) && is_string($value)){
            $el = $dom->createElement($key, $value);
            $node->appendChild($el);
        }
    }
}


#implementation

    $doc = new DOMDocument('1.0', 'UTF-8');

    $doc->formatOutput = true;

    $pre = $doc->createElement('PRE');


    #<TRNXML Version="1.0.0" TimeZone="ET" />
    $TRNXML = $doc->createElement('TRNXML');
    $TRNXML->setAttribute('Version', '1.0.0');
    $TRNXML->setAttribute('TimeZone', 'ET');
    $pre->appendChild($TRNXML);

    $array = array (
            'DateTimeStamp' => '06/30/2017 08:11:23',
            'Sender' => array   (
                    'SenderID' => 'TRN',
                    'SenderName' => 'Transportation Reservation System,Inc.'),
            'Recipient' => array(
                    'RecipientID' => 'DATA1',
                    'RecipientName' => ''   ),
            'Payload' => array (
                    'RateProduct' => array(
                            0 => array(
                                    'RateVendor' => 42074,
                                    'RateId' => 13,
                                    'RentalLocationID' => 262,
                                    'RateCompanyID' => 'RCR',
                                    'TotalPricing' => array(
                                            'RentalDays' => 1,
                                            'RateCharge' => 127.50
                                            ),
                                    'Taxes' => array(
                                            'Tax2Amount' => 154.04,
                                            'Tax2Rate' => 0.13
                                            ),
                                    0 => array(
                                            'DailyExtra' => array(
                                                    'ExtraCode' => 'ERF',
                                                    'ExtraDesc' => 'ENERGY RECOVERY FEES'
                                                    )
                                          ),
                                    1 => array(
                                            'DailyExtra' => array(
                                                    'ExtraCode' => 'ERF',
                                                    'ExtraDesc' => 'ENERGY RECOVERY FEES'
                                                    )
                                            )
                                    ),
                            1 => array(
                                    'RateVendor' => 42074,
                                    'RateId' => 13,
                                    'RentalLocationID' => 262,
                                    'RateCompanyID' => 'RCR',
                                    'TotalPricing' => array(
                                            'RentalDays' => 1,
                                            'RateCharge' => 127.50
                                            ),
                                    'Taxes' => array(
                                            'Tax2Amount' => 154.04,
                                            'Tax2Rate' => 0.13
                                            ),
                                    0 => array(
                                            'DailyExtra' => array(
                                                    'ExtraCode' => 'ERF',
                                                    'ExtraDesc' => 'ENERGY RECOVERY FEES'
                                                    )

                                            ),

                                    1 => array(
                                            'DailyExtra' => array (
                                                    'ExtraCode' => 'ERF',
                                                    'ExtraDesc' => 'ENERGY RECOVERY FEES'
                                                    )
                                            )
                                    )
                            )
                    )
            );

    array_to_xml($array, $doc, $pre);

    $doc->appendChild($pre);

echo $doc->saveXML() . "\n";

答案 1 :(得分:0)