dousi1961 2014-11-03 06:43
浏览 65
已采纳

跟进:xml到php数组的正确转换是什么? [重复]

This question is an exact duplicate of:

Follow up question to this How to convert properly the xml radio into php array

I have a format of xml (output) and I want to convert it into PHP Array (input). But my codes it's not working, it only display the whole code of my array when I try to run it.

How to do the right conversion?

I have this xml with radio button:

<?xml version="1.0" encoding="UTF-8"?>
<Form xmlns="url" label="Home Visit Form" type="TextView" name="Form">
<Field name="ClientRelation" label="Client" value="Kamosi" type="TextView"/>
<Field name="ContactNum" label="ContactNum" value="12345678" type="TextView"/>
<Field name="Address" label="Address" value="tes" type="TextView"/>
 <Field name="Landmark" label="Landmark" value="Pred dubem, za dubem" type="TextView"/>
 <Field type="Delimiter"/>
 <Field name="CP_RESULT" label="Cash pick-up result" value="CP_RESULT_PAID_TO_VISITOR" type="Radio">
<Item code="CP_RESULT_PAID_TO_VISITOR" label="Client paid to field visitor"/>
<Item code="CP_RESULT_PAID_THRU_PMT_CHANNEL" label="Client paid through payment channel"/>
<Item code="CP_RESULT_CLIENT_NOT_AVAIL" label="Client not available"/>
<Item code="CP_RESULT_CLIENT_CAN_NOT_PAY" label="Client can not pay"/>
</Field>
<Field name="CP_INFO_FROM_PAYEE" label="Notes" value="fgjjk" type="EditText" format="string"/>
<Field name="CP_PICTURE_RECEIPT" label="Receipt picture" value="Cash Pick-up 3400888888-CP_PICTURE_RECEIPT.jpg" type="Picture" format="string"/>
<Field name="CP_PICTURE_PLACE" label="Place picture (outdoor)" value="Cash Pick-up 3400888888-CP_PICTURE_PLACE.jpg" type="Picture" format="string"/>
</Form>

PHP Array:

require_once('XMLtoArray.php');
$Example1= array();
$Example1['@attributes'] = array(
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'xsi:noNamespaceSchemaLocation' => 'url',
'label'=> 'Form',
'type' => 'TextView',
'name' => 'Form1'
 );

 'Field' => array(
    array(
    '@attributes' => array(
        'name' => 'ClientRelation'
        'label => 'test'
        'value' => '',
        'type' => 'TextView'
),
    array(
    '@attributes' => array(
        'name' => '(
        'name' => 'ContactNum'
        'label => 'ContactNum'
        'value' => '',
        'type' => 'TextView'
),
    array(
    '@attributes' => array(
        'name' => 'Address'
        'label => 'Address'
        'value' => '',
        'type' => 'TextView'
),
    array(
    '@attributes' => array(
        'name' => 'Landmark'
        'label => 'Landmark'
        'value' => '',
        'type' => 'TextView'
),
    array(
    '@attributes' =>(
        'type' = > 'Delimiter'
),
    array(
    '@attributes' => array(
        'name' => 'CP_RESULT'
        'label => 'Cash pick-up result'
        'value' => '',
        'type' => 'Radio'
),
'Item' => array(
    array(
    '@attributes' => array(
    'code' => 'CP_RESULT_PAID_TO_VISITOR',
    'label' => 'Client paid to field visitor',
),
    array(
    '@attributes' => array(
    'code' => 'CP_RESULT_PAID_THRU_PMT_CHANNEL',
    'label' => 'Client paid through payment channel',
),
    array(
    '@attributes' => array(
    'code' => 'CP_RESULT_CLIENT_NOT_AVAIL',
    'label' => 'Client not available',
),
    array(
    '@attributes' => array(
    'code' => 'CP_RESULT_CLIENT_CAN_NOT_PAY',
    'label' => 'Client can not pay',
);
    array(
    '@attributes' => array(
        'name' => 'CP_PICKED_AMOUNT'
        'label => 'Picked Amount'
        'value' => '',
        'type' => 'EditText',
        'format' => 'string'
),
    array(
    '@attributes' => array(
        'name' => '(
        'name' => 'CP_VISIT_DATE'
        'label => 'Date of Pick-up'
        'value' => '',
        'type' => 'EditText',
        'format' => 'string'
),
    array(
    '@attributes' => array(
        'name' => 'CP_VISIT_TIME'
        'label => 'Time of Pick-up'
        'value' => '',
        'type' => 'EditText',
        'format' => 'string'
),
    array(
    '@attributes' => array(
        'name' => 'CP_REASON_CODE'
        'label => 'Reason Code'
        'value' => '',
        'type' => 'EditText',
        'format' => 'string'
),
    array(
    '@attributes' => array(
        'name' => '(
        'name' => 'CP_NOTES'
        'label => 'Notes'
        'value' => '',
        'type' => 'EditText',
        'format' => 'string'
),
    array(
    '@attributes' => array(
        'name' => 'CP_RECEIPT_NUMBER'
        'label => 'Receipt number'
        'value' => '',
        'type' => 'EditText',
        'format' => 'string'
),
    array(
    '@attributes' => array(
        'name' => 'CP_INFO_FROM_PAYEE'
        'label => 'Notes'
        'value' => '',
        'type' => 'EditText',
        'format' => 'string'
),
  array(
    '@attributes' => array(
        'name' => 'CP_PICTURE_RECEIPT'
        'label => 'Receipt picture'
        'value' => '',
        'type' => 'Picture
        'format' => 'string'
),
    array(
    '@attributes' => array(
        'name' => 'CP_PICTURE_PLACE'
        'label => 'Place picture (outdoor)'
        'value' => '',
        'type' => 'Picture',
        'format' => 'string'
);

$xml = Array2XML::createXML('CashPickUp', $CashPickUp);
echo $xml->saveXML();

?>

XMLtoArray.php

<?php

class XML2Array {

private static $xml = null;
private static $encoding = 'UTF-8';

public static function init($version = '1.0', $encoding = 'UTF-8', $format_output = true) {
    self::$xml = new DOMDocument($version, $encoding);
    self::$xml->formatOutput = $format_output;
    self::$encoding = $encoding;
}

public static function &createArray($input_xml) {
    $xml = self::getXMLRoot();
    if(is_string($input_xml)) {
        $parsed = $xml->loadXML($input_xml);
        if(!$parsed) {
            throw new Exception('[XML2Array] Error parsing the XML string.');
        }
    } else {
        if(get_class($input_xml) != 'DOMDocument') {
            throw new Exception('[XML2Array] The input XML object should be of type: DOMDocument.');
        }
        $xml = self::$xml = $input_xml;
    }
    $array[$xml->documentElement->tagName] = self::convert($xml->documentElement);
    self::$xml = null;    // clear the xml node in the class for 2nd time use.
    return $array;
}

private static function &convert($node) {
    $output = array();

    switch ($node->nodeType) {
        case XML_CDATA_SECTION_NODE:
            $output['@cdata'] = trim($node->textContent);
            break;

        case XML_TEXT_NODE:
            $output = trim($node->textContent);
            break;

        case XML_ELEMENT_NODE:

            // for each child node, call the covert function recursively
            for ($i=0, $m=$node->childNodes->length; $i<$m; $i++) {
                $child = $node->childNodes->item($i);
                $v = self::convert($child);
                if(isset($child->tagName)) {
                    $t = $child->tagName;

                    // assume more nodes of same kind are coming
                    if(!isset($output[$t])) {
                        $output[$t] = array();
                    }
                    $output[$t][] = $v;
                } else {
                    //check if it is not an empty text node
                    if($v !== '') {
                        $output = $v;
                    }
                }
            }

            if(is_array($output)) {
                // if only one node of its kind, assign it directly instead if array($value);
                foreach ($output as $t => $v) {
                    if(is_array($v) && count($v)==1) {
                        $output[$t] = $v[0];
                    }
                }
                if(empty($output)) {
                    //for empty nodes
                    $output = '';
                }
            }

            // loop through the attributes and collect them
            if($node->attributes->length) {
                $a = array();
                foreach($node->attributes as $attrName => $attrNode) {
                    $a[$attrName] = (string) $attrNode->value;
                }
                // if its an leaf node, store the value in @value instead of directly storing it.
                if(!is_array($output)) {
                    $output = array('@value' => $output);
                }
                $output['@attributes'] = $a;
            }
            break;
    }
    return $output;
}

/*
 * Get the root XML node, if there isn't one, create it.
 */
private static function getXMLRoot(){
    if(empty(self::$xml)) {
        self::init();
    }
    return self::$xml;
}
}
?>
</div>
  • 写回答

1条回答 默认 最新

  • duangong0690 2014-11-03 07:14
    关注

    What about simple:

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

    According to https://stackoverflow.com/a/20431742/1856120

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里