PROBLEMA
- funzione PHP
simplexml_load_file
sta gettando errore parser error : xmlParseEntityRef
analisi durante il tentativo di caricare il file XML da un URL.
CAUSA
- XML restituito dal URL non è un XML valido. Esso contiene
&
valore anziché &
. È del tutto possibile che ci siano altri errori che non sono ovvi in questo momento.
COSE fuori dal nostro controllo
- Idealmente, dovremmo fare in modo che un XML valido viene alimentata nel funzione PHP
simplexml_load_file
, ma sembra che non abbiamo alcuna controllo su come viene creato l'XML.
- Non è inoltre possibile forzare
simplexml_load_file
per elaborare un file XML non valido . Non ci lascia molte opzioni, a parte lo che fissa il file XML stesso.
SOLUZIONE POSSIBILE
Converti XML non valido per XML valido. Può essere fatto usando PHP tidy extension
. Ulteriori istruzioni possono essere trovate da http://php.net/manual/en/book.tidy.php
Una volta accertato che l'estensione esiste o è installata, eseguire le operazioni seguenti.
/**
* As per the question asked, the URL is loaded into a variable first,
* which we can assume to be $xml
*/
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<project orderno="6" campaign_name="International Relief & Development for under developed nations">
<invalid-data>Some other data containing & in it</invalid-data>
<unclosed-tag>
</project>
XML;
/**
* Whenever we use tidy it is best to pass some configuration options
* similar to $tidyConfig. In this particular case we are making sure that
* tidy understands that our input and output is XML.
*/
$tidyConfig = array (
'indent' => true,
'input-xml' => true,
'output-xml' => true,
'wrap' => 200
);
/**
* Now we can use tidy to parse the string and then repair it.
*/
$tidy = new tidy;
$tidy->parseString($xml, $tidyConfig, 'utf8');
$tidy->cleanRepair();
/**
* If we try to output the repaired XML string by echoing $tidy it should look like.
<?xml version="1.0" encoding="utf-8"?>
<project orderno="6" campaign_name="International Relief & Development for under developed nations">
<invalid-data>Some other data containing & in it</invalid-data>
<unclosed-tag></unclosed-tag>
</project>
* As you can see that & is now fixed in campaign_name attribute
* and also with-in invalid-data element. You can also see that the
* <unclosed-tag> which didn't had a close tag, has been fixed too.
*/
echo $tidy;
/**
* Now when we try to use simplexml_load_string to load the clean XML. When we
* try to print_r it should look something like below.
SimpleXMLElement Object
(
[@attributes] => Array
(
[orderno] => 6
[campaign_name] => International Relief & Development for under developed nations
)
[invalid-data] => Some other data containing & in it
[unclosed-tag] => SimpleXMLElement Object
(
)
)
*/
$simpleXmlElement = simplexml_load_string($tidy);
print_r($simpleXmlElement);
ATTENZIONE
Lo sviluppatore dovrebbe cercare di confrontare il codice XML non valido con un XML valido (generato da ordine), per vedere non ci sono effetti collaterali dopo l'uso ordinato. Tidy fa un ottimo lavoro nel farlo correttamente, ma non fa mai male vederlo visivamente e per essere sicuro al 100%. Nel nostro caso dovrebbe essere semplice come confrontare $ xml con $ tidy.
Il XML non è valido. Potresti non essere in grado di caricarlo affatto. Gli errori possono essere soppressi aggiungendo '@' davanti a 'simplexml_load_file' o aggiungendo un flag, vedi la pagina man di' simplexml_load_file' per maggiori informazioni e per favore elimina la tua domanda, è un duplicato. – hakre
Vedo che la mia risposta sta ricevendo molta attenzione, se questa è effettivamente la soluzione: puoi contrassegnarla come "risposta corretta"? Grazie. – ricricucit