資源:PHP official site- SimpleXMLElement documentation
如果您聲稱有在這一行錯誤:
$xml = iconv("UTF-16", "UTF-8", $xml);
然後將其更改到這一點,因爲$ XML可能不是 「UTF-16」:
$xml = iconv(mb_detect_encoding($xml), "UTF-8", $xml);
要保存XML文件:
//saving generated xml file
$xml_student_info->asXML('file path and name');
要導入XML文件:
$url = "http://www.domain.com/users/file.xml";
$xml = simplexml_load_string(file_get_contents($url));
如果你有一個數組如下:
$test_array = array (
'bla' => 'blub',
'foo' => 'bar',
'another_array' => array (
'stack' => 'overflow',
),
);
,並希望將其轉換爲以下XML:
<?xml version="1.0"?>
<main_node>
<bla>blub</bla>
<foo>bar</foo>
<another_array>
<stack>overflow</stack>
</another_array>
</main_node>
那麼這裏PHP代碼:
<?php
//make the array
$test = array (
'bla' => 'blub',
'foo' => 'bar',
'another_array' => array (
'stack' => 'overflow',
),
);
//make an XML object
$xml_test = new SimpleXMLElement("<?xml version=\"1.0\"?><main_node></main_node>");
// function call to convert array to xml
array_to_xml($test,$xml_test);
//here's the function definition (array_to_xml)
function array_to_xml($test, &$xml_test) {
foreach($test as $key => $value) {
if(is_array($value)) {
if(!is_numeric($key)){
$subnode = $xml_test->addChild("$key");
array_to_xml($value, $subnode);
}
else{
$subnode = $xml_test->addChild("item$key");
array_to_xml($value, $subnode);
}
}
else {
$xml_test->addChild("$key","$value");
}
}
}
/we finally print it out
print $xml_test->asXML();
?>
使用ASP Classic腳本以unicode格式製作XML。 XML使用PHP腳本編寫「UTF-8」或「ANSI」。 –