SimpleXML and its Not So Simple To Use Object Model: Problem & Solution SimpleXML e la sua non è così semplice da usare Object Model: Problema & Soluzione
SimpleXML is simple to use as its name suggests. SimpleXML è semplice da utilizzare come suggerisce il suo nome. However it is a pain when you need to check the value of an element which may or may not exist. Tuttavia si tratta di un dolore, quando è necessario controllare il valore di un elemento che può o non può esistere.
In short using it on XML schema with optional elements is a royal pain. In breve utilizzarlo su schema XML con elementi facoltativi è un dolore reale. As SimpleXML only creates objects for which data exists, you will have to write ugly code everytime to check for existence of an object before you access its value. Come SimpleXML crea solo oggetti per i quali esistono dati, sarà necessario scrivere codice brutto everytime per verificare l'esistenza di un oggetto prima di accesso il suo valore.
You cannot solve it by simply writing a convenience method. Non è possibile risolverlo semplicemente scrivendo un metodo comodo. Lets understand the problem and then we will solve it. Consente di capire il problema e poi risolverlo.
The Advantage Il vantaggio
SimpleXML is very simple to use. SimpleXML è molto semplice da usare. You access elements as you would access an Object hierarchy: Si accede a elementi come se fosse un oggetto di accesso struttura gerarchica:
$xml->Items[0]->Request[0]->Errors[0]->Error[0]->Status $ xml-> Items [0] -> Richiesta [0] -> errori [0] -> Errore [0] -> Stato
The Problem Il Problema
SimpleXML creates objects only for XML elements which have data (and rightly so). SimpleXML crea oggetti solo per elementi XML che sono i dati (e giustamente). This makes it efficient. Ciò rende efficiente. However it also makes it harder to use when your schema is flexible with optional elements. Tuttavia si rende anche più difficile da usare quando il tuo schema è flessibile con elementi facoltativi. Lets take the above example. Consente di prendere l'esempio precedente. You cannot safely access Status as above if Errors is an optional element which is only present when there is an error. Non è possibile accedere in sicurezza di stato come sopra se gli errori è un elemento opzionale che è presente solo quando vi è un errore. And you cannot check the existence of Status like this: E non è possibile controllare l'esistenza di Stato in questo modo:
isset($xml->Items[0]->Request[0]->Errors[0]->Error[0]->Status) isset ($ xml-> Items [0] -> Richiesta [0] -> errori [0] -> Errore [0] -> Stato)
because Errors may not be present. perché gli errori non possono essere presenti. So to find out if there are any error you first have to check the existence of Errors and then the existance of Status (possibly) before you can actually check the status. Così per sapere se ci sono errori, dovete prima di verificare l'esistenza di errori e quindi l'esistenza di Stato (eventualmente) prima di poter effettivamente controllare lo stato. And this is true for any depth of the object hierarchy. E questo è vero per qualsiasi profondità l'oggetto di gerarchia. You can see how quickly this cane become a major pain in the neck. Si può vedere quanto velocemente la canna da diventare un grande dolore nel collo. Also you are very likely to miss some and cause maintenance hassles. Vi sono anche molto probabile perdere e causare alcuni problemi di manutenzione. The code becomes unmaintainable too. Il codice diventa troppo unmaintainable.
Solution Soluzione
My solution is to use XPath for such situations. La mia soluzione è quella di utilizzare XPath per tali situazioni. It succintly tells you the availability and value (if available) of a field. Succintly che ti dice la disponibilità e il valore (se disponibile) di un campo.
Conclusion Conclusione
SimpleXML in conjunction with XPath is a very powerful way to process your XML data in PHP. SimpleXML, in combinato disposto con XPath è un modo molto potente per elaborare la vostra dati XML in PHP.
Filed under Elencato sotto Headline News Headline News , How To Come , PHP , Tech Note Nota tech , Technology Tecnologia , Web , Web Services Servizi Web | |
| |
RSS 2.0 RSS 2,0 | |
Trackback this Article | questo articolo |
Email this Article Invia questo articolo
You may also like to read Si può anche leggere come |





November 16th, 2005 at 11:42 pm 16 novembre 2005 alle 11:42 pm
[...] Ones SimpleXML and its Not So Simple To Use Object Model: Problem & Solution » SimpleXML doesn’t Work with PHP 5.x: Problem & Solution November 16th, 2005 by AngsumanChakraborty [...] [...] Ones SimpleXML e la sua Not So semplice da usare Object Model: Problema & Soluzione »SimpleXML non funziona con PHP 5.x: Problema & Soluzione 16 novembre 2005 da AngsumanChakraborty [...]
November 22nd, 2005 at 8:37 am 22 novembre 2005 alle 8:37 am
[...] Reports Sony DRM Rootkit violating several open source licenses, lists Christmas wish from Google AdSense, provides tips on Google Analytics and simplifies SimpleXML for PHP developers. [...] Rapporti Sony DRM Rootkit violare diverse licenze open source, elenchi di Natale dal desiderio di AdSense di Google, fornisce suggerimenti su Google Analytics e semplifica SimpleXML per gli sviluppatori PHP. [...]
November 22nd, 2005 at 8:42 am 22 novembre 2005 alle 8:42 am
[...] Reports Sony DRM Rootkit violating several open source licenses, lists Christmas wish from Google AdSense, provides tips on Google Analytics and simplifies SimpleXML for PHP developers. [...] Rapporti Sony DRM Rootkit violare diverse licenze open source, elenchi di Natale dal desiderio di AdSense di Google, fornisce suggerimenti su Google Analytics e semplifica SimpleXML per gli sviluppatori PHP. [...]
December 19th, 2005 at 2:55 am 19 dicembre 2005 a 2:55 am
[...] Reports Sony DRM Rootkit violating several open source licenses, lists Christmas wish from Google AdSense, provides tips on Google Analytics and simplifies SimpleXML for PHP developers. [...] Rapporti Sony DRM Rootkit violare diverse licenze open source, elenchi di Natale dal desiderio di AdSense di Google, fornisce suggerimenti su Google Analytics e semplifica SimpleXML per gli sviluppatori PHP. [...]