跟进此How to convert properly the xml radio into php array
的问题我有一个xml格式(输出),我想将其转换为PHP数组(输入)。但是我的代码不起作用,它只在我尝试运行它时显示我的数组的整个代码。
如何进行正确的转换?
我有这个带有单选按钮的xml:
<?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数组:
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;
}
}
?>
答案 0 :(得分:1)
如何简单:
$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);
$array = json_decode($json,TRUE);