PHP生成XML文件,<>被转义,怎么才能让它不转义呢?
$xmlstring = '<?xml version="1.0" encoding="utf-8"?><DOCUMENT></DOCUMENT>';
$xml = simplexml_load_string($xmlstring);
foreach($data as $v){
$array = array(
'item'=>array(
'key'=>'<![CDATA['.$key.']]>',
'display'=>array(
'question'=> '<![CDATA['.$title.']]>',
'tag1'=>'<![CDATA['.$tag1.']]>',
'tag2'=>'<![CDATA['.$tag2.']]>',
'id'=>'<![CDATA[2]]>',
'title'=>'<![CDATA['.$title.']]>',
'url'=>'<![CDATA['.$wapUrl.']]>',
'source'=>'<![CDATA['.$webname.']]>',
'source_logo'=>'<![CDATA['.$wapUrl.'favicon.jpg]]>',
'source_slogan'=>'<![CDATA['.$source_slogan.']]>',
'answerinfo'=>array(
'article'=>array(
'article_url'=>'<![CDATA['.$wapUrl.']]>'
)
),
)
)
);
$this->array_to_xml($array, $xml);
}
$r = $xml->asXML($xmlpath);
// 递归地将数组转换为XML
function array_to_xml($array, &$xml) {
foreach($array as $key => $value) {
if(is_array($value)) {
if(!is_numeric($key)){
$subnode = $xml->addChild("$key");
$this->array_to_xml($value, $subnode);
} else {
$this->array_to_xml($value, $xml);
}
} else {
$xml->addChild("$key",$value);
//$xml->addChild("$key",htmlspecialchars($value, ENT_XML1));
}
}
}
得到的XML,打开后: