2010-07-02 8 views
7

Ich lade einige Informationen mit cURL im XML-Format.

.... 

$xml = curl_exec($ch); 

$data = simplexml_load_string($xml); 
print_r($data); 
//out put - SimpleXMLElement Object () 

wenn ich versuche - print_r($xml); und Ansichtsseite Quelle ich

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
    <ns7:users xmlns="http://www.example.com/xml/ns/rs" 
     xmlns:ns2="http://www.example.com/xml/ns/users" 
     xmlns:ns3="http://www.example.com/2004/11/tHistory" 
     xmlns:ns4="http://www.example.com/fsi/tHistory" 
     xmlns:ns5="http://www.example.com/2005/10/tHistory" 
     xmlns:ns6="http://www.example.com/2010/03/cs" 
     xmlns:ns7="http://www.example.com/2005/10/users" 
     xmlns:ns8="http://www.example.com/2010/03/tHistory"> 
    <ns7:user><ns7:id>Matt.Smith</ns7:id> 
    <ns7:lastName>Smith</ns7:lastName> 
    <ns7:firstName>Matt</ns7:firstName> 
    <ns7:otherName></ns7:otherName> 
    <ns7:gender>male</ns7:gender> 
    <ns7:email>[email protected]</ns7:email> 
    <ns7:locale>en</ns7:locale> 
    <ns7:role><ns7:id>A</ns7:id> 
    <ns7:name>System Administrator</ns7:name></ns7:role> 
    <ns7:employeeNumber></ns7:employeeNumber> 
    <ns7:organization> 
     <ns7:id>8000</ns7:id> 
     <ns7:name>Organisation Title</ns7:name> 
    </ns7:organization> 
    <ns7:organization> 
     <ns7:id>20707</ns7:id> 
     <ns7:name>London Office</ns7:name> 
    </ns7:organization> 
    <ns7:attribute> 
     <ns7:code>0</ns7:code> 
     <ns7:description>Unassigned</ns7:description> 
    </ns7:attribute> 
    <ns7:attribute> 
     <ns7:code>0</ns7:code> 
     <ns7:description>Unassigned</ns7:description> 
    </ns7:attribute> 
    <ns7:attribute> 
     <ns7:code></ns7:code> 
     <ns7:description>Unassigned</ns7:description> 
    </ns7:attribute> 
    <ns7:attribute> 
     <ns7:code></ns7:code> 
     <ns7:description>Unassigned</ns7:description></ns7:attribute> 
     <ns7:attribute><ns7:code></ns7:code> 
     <ns7:description>Unassigned</ns7:description> 
    </ns7:attribute> 
    <ns7:attribute> 
     <ns7:code></ns7:code> 
     <ns7:description>Unassigned</ns7:description> 
     </ns7:attribute> 
    <ns7:attribute> 
     <ns7:code></ns7:code> 
     <ns7:description>Unassigned</ns7:description> 
    </ns7:attribute> 
    <ns7:attribute> 
     <ns7:code></ns7:code> 
     <ns7:description>Unassigned</ns7:description> 
    </ns7:attribute> 
    </ns7:user> 
</ns7:users> 

diese xml alles in einer Zeile ist, und ich habe manuell eingegeben Zeile lesbar machen bricht.

Antwort

12

UPDATE: drucken Vorname (oder andere), können Sie the usual SimpleXML addressing mechanisms verwenden. Ihr Fall ist ein wenig komplizierter, weil Sie Namespaces verwenden. obwohl noch praktikabel - versuchen, etwas like this:

$data->children('ns7', true)->user[0]->lastName 

re: i print_r($data) erwarte zu drucken, als ob es ein Array war [...]: Diese Erwartung ist falsch. es wäre sicherlich praktisch, aber so funktioniert es nicht. Um die XML-Zeichenfolgendarstellung eines SimpleXML-Objekts zu drucken, verwenden Sie asXML().

UPDATE END

, was man erwartet print_r($data) zu drucken? SimpleXMLElement Object () scheint mir eine vollkommen gültige Ausgabe zu sein. es bedeutet nicht, dass etwas mit der XML falsch ist. Wenn Sie die tatsächliche XML-Datei Ihres SimpleXMLElement-Objekts sehen möchten, versuchen Sie print $data->asXML().

+0

Danke irgendwelche Ideen, wie man Vorname druckt? oder irgend ein anderer? – user187580

+0

Ich erwarte zu print_r ($ Daten) zu drucken, als wäre es ein Array .. 'Vorname' => Vorname Wert etc. Es funktionierte ok für mich, aber vielleicht einige Änderungen auf XML-Datenquelle machte es so leer. – user187580

+0

sehe meine aktualisierte Antwort. –

5

Nun, das ist kein leeres Objekt. In der Tat, wenn Sie print_r es zeigt, was Sie uns gezeigt haben. Aber wenn Sie zum Beispiel tun

echo $data->asXML(); 

das Ergebnis korrekt ist:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<ns7:users xmlns="http://www.example.com/xml/ns/rs" xmlns:ns2="http://www.example.com/xml/ns/users" xmlns:ns3="http://www.example.com/2004/11/tHistory" xmlns:ns4="http://www.example.com/fsi/tHistory" xmlns:ns5="http://www.example.com/2005/10/tHistory" xmlns:ns6="http://www.example.com/2010/03/cs" xmlns:ns7="http://www.example.com/2005/10/users" xmlns:ns8="http://www.example.com/2010/03/tHistory"> 
    <ns7:user><ns7:id>Matt.Smith</ns7:id> 
    <ns7:lastName>Smith</ns7:lastName> 
    <ns7:firstName>Matt</ns7:firstName> 
    <ns7:otherName/> 
    <ns7:gender>male</ns7:gender> 
    <ns7:email>[email protected]</ns7:email> 
    <ns7:locale>en</ns7:locale> 
    <ns7:role><ns7:id>A</ns7:id> 
    <ns7:name>System Administrator</ns7:name></ns7:role> 
    <ns7:employeeNumber/> 
... 

einfach das Objekt verwenden, wie SimpleXML gemeint ist :)

Um zu überprüfen, ob es korrekt geladen wird, sehen die doc:

Fehler/Exceptions

Erzeugt für jeden in den XML-Daten gefundenen Fehler eine E_WARNING-Fehlermeldung und löst eine Ausnahme aus, wenn Fehler erkannt wurden.

bei page

+0

Danke in der Tat funktioniert :) Wie drucke ich Vorname, Nachname dann? – user187580

+0

Nun, wie in AXs Antwort :) –

0

Oder var Dump den ns7 entfernen von Knoten Namensräume, lassen sie auf der Wurzel:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
    <ns7:users xmlns="http://www.example.com/xml/ns/rs" 
     xmlns:ns2="http://www.example.com/xml/ns/users" 
     xmlns:ns3="http://www.example.com/2004/11/tHistory" 
     xmlns:ns4="http://www.example.com/fsi/tHistory" 
     xmlns:ns5="http://www.example.com/2005/10/tHistory" 
     xmlns:ns6="http://www.example.com/2010/03/cs" 
     xmlns:ns7="http://www.example.com/2005/10/users" 
     xmlns:ns8="http://www.example.com/2010/03/tHistory"> 
    <user><id>Matt.Smith</id> 
    <lastName>Smith</lastName> 
    <firstName>Matt</firstName> 
    <otherName></otherName> 
    <gender>male</gender> 
    <email>[email protected]</email> 
    <locale>en</locale> 
    <role><id>A</id> 
    <name>System Administrator</name></role> 
    <employeeNumber></employeeNumber> 
    <organization> 
     <id>8000</id> 
     <name>Organisation Title</name> 
    </organization> 
    <organization> 
     <id>20707</id> 
     <name>London Office</name> 
    </organization> 
    <attribute> 
     <code>0</code> 
     <description>Unassigned</description> 
    </attribute> 
    <attribute> 
     <code>0</code> 
     <description>Unassigned</description> 
    </attribute> 
    <attribute> 
     <code></code> 
     <description>Unassigned</description> 
    </attribute> 
    <attribute> 
     <code></code> 
     <description>Unassigned</description></attribute> 
     <attribute><code></code> 
     <description>Unassigned</description> 
    </attribute> 
    <attribute> 
     <code></code> 
     <description>Unassigned</description> 
     </attribute> 
    <attribute> 
     <code></code> 
     <description>Unassigned</description> 
    </attribute> 
    <attribute> 
     <code></code> 
     <description>Unassigned</description> 
    </attribute> 
    </user> 
</ns7:users> 
+0

danke .. Irgendwelche Ideen, wie Vorname, Nachname usw. zugreifen? – user187580

+0

Da es sich um ein Objekt handelt, können Sie die Standard-Objektreferenz verwenden: $ data -> user-> firstName – Ashley

0

Ja ich hatte das gleiche Problem und dachte, dass die simplexml_load_string zurückkehrte leer, da print_r ($ data) oder echo $ data gerade leer sind.

aber wenn Sie $ data-> name tun dann erhalten Sie eine gültige Daten ..it irgendwie wierd aber das, wie es funktioniert .. so toll Tipp .. danke ..es funktionierte für mich

Verwandte Themen