2013-10-05 10 views
6

Quando si genera un file XML con uno script ASP classico e si importa il file XML in una pagina PHP, il processo di importazione funziona correttamente.Errore iconv PHP

Tuttavia, quando si genera lo stesso XML tramite uno script PHP (anziché ASP Classic) e lo si utilizza nello stesso processo di importazione, non funziona.

$xml = iconv("UTF-16", "UTF-8", $xml); 

ho notato nel mio processo di importazione:

  • prima della linea $xml = iconv("UTF-16", "UTF-8", $xml); nel mio codice, il file XML è nel formato corretto.
  • Ma dopo la riga $xml = iconv("UTF-16", "UTF-8", $xml);, il file XML è danneggiato.

Quando commento questa riga di codice e utilizzo il file XML PHP, quindi funziona correttamente.

+0

XML realizzato in formato Unicode con script ASP Classic. E XML ha realizzato "UTF-8" o "ANSI" con script PHP. –

risposta

0

Cosa succede quando si esegue:

$xml = iconv("UTF-16", "UTF-8//IGNORE", $xml); 

?

Se il processo non riesce nel punto identificato, la conversione da UTF-16 a UTF-8 fallisce, il che significa che uno o più caratteri nella stringa di input non hanno un UTF- 8 rappresentazione. Il flag "// IGNORE" farà cadere silenziosamente quei caratteri, il che è ovviamente negativo, ma l'utilizzo di tale flag può essere utile per individuare se ciò che penso sia effettivamente il problema. Puoi anche provare a tradurre i caratteri che non funzionano:

$xml = iconv("UTF-16", "UTF-8//TRANSLIT", $xml); 

I caratteri saranno approssimati, quindi manterrai qualcosa, almeno. Vedere l'esempio qui: http://www.php.net/manual/en/function.iconv.php

Tutto ciò detto, UTF-16 è un set di caratteri accettabile per il contenuto XML. Perché vuoi convertirlo?

4

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(); 

?>