imcnan
2018-05-15 10:35:43 +08:00
```php
public function arrayToXML($arr) {
$xml = "";
foreach ($arr as $key => $val) {
$node = "";
if ( is_string($val) || is_int($val) || is_double($val) || is_float($val) ) {
$val = htmlspecialchars($val);
$node = "{$val}{$key}>";
} else if (is_array(val)) {
die("不应该有这个参数的");
}
$xml .= $node;
$xml .= "\n";
}
$xml .= "";
return $xml;
}
private function xmlToArray($xml) {
/*
*/
$ret = array();
$parser = xml_parser_create("UTF-8");
$values = array();
$keys = array();
xml_parse_into_struct($parser, $xml, $values, $keys);
foreach ($keys as $nodeName => $val) {
$id = $val[0];
if (!empty($values[$id]["value"])) {
$nodeName = strtoupper($nodeName);
$ret[$nodeName] = $values[$id]["value"];
}
}
return $ret;
}
```