risorse: PHP official site- SimpleXMLElement documentation
Se si sostiene che c'è un errore in questa linea:
$xml = iconv("UTF-16", "UTF-8", $xml);
poi cambiare a questo, perché $ xml non è probabilmente "UTF-16":
$xml = iconv(mb_detect_encoding($xml), "UTF-8", $xml);
Per salvare il file XML:
//saving generated xml file
$xml_student_info->asXML('file path and name');
Per importare il file xml:
$url = "http://www.domain.com/users/file.xml";
$xml = simplexml_load_string(file_get_contents($url));
Se si dispone di un array come segue:
$test_array = array (
'bla' => 'blub',
'foo' => 'bar',
'another_array' => array (
'stack' => 'overflow',
),
);
e si desidera convertire al seguente XML:
<?xml version="1.0"?>
<main_node>
<bla>blub</bla>
<foo>bar</foo>
<another_array>
<stack>overflow</stack>
</another_array>
</main_node>
allora ecco il codice 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();
?>
XML realizzato in formato Unicode con script ASP Classic. E XML ha realizzato "UTF-8" o "ANSI" con script PHP. –