2010-07-02 16 views
7

से नीचे कोड मैं xml प्रारूप में curl का उपयोग कर कुछ जानकारी पुनर्प्राप्त करता हूं।

.... 

$xml = curl_exec($ch); 

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

अगर मैं कोशिश - print_r($xml); और देखने पृष्ठ स्रोत मैं

<?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> 

प्राप्त इस एक्सएमएल सब एक कतार में है और मैं मैन्युअल रूप से दर्ज किया है लाइन यह पठनीय बनाने के लिए टूट जाता है।

उत्तर

12

अद्यतन: firstname (या किसी अन्य) मुद्रित करने के लिए, आप the usual SimpleXML addressing mechanisms उपयोग कर सकते हैं। आपका मामला थोड़ा और जटिल है क्योंकि आप नेमस्पेस का उपयोग कर रहे हैं। - अभी भी व्यावहारिक हालांकि कुछ like this कोशिश:

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

पुन: मैं print_r($data) उम्मीद कर रहा हूँ मुद्रित करने के लिए जैसे कि यह एक सरणी थे [...]: इस उम्मीद गलत है। यह निश्चित रूप से आसान होगा, लेकिन ऐसा नहीं है कि यह कैसे काम करता है। एक SimpleXML ऑब्जेक्ट के xml स्ट्रिंग प्रस्तुति को मुद्रित करने के लिए, asXML() का उपयोग करें।

अद्यतन अंत

आप उम्मीद कर रहे हैं क्या print_r($data) मुद्रित करने के लिए? SimpleXMLElement Object () मेरे लिए पूरी तरह से मान्य आउटपुट प्रतीत होता है। इसका मतलब यह नहीं है कि एक्सएमएल के साथ कुछ गड़बड़ है। अगर आप अपने SimpleXMLElement ऑब्जेक्ट का वास्तविक एक्सएमएल देखना चाहते हैं, तो print $data->asXML() आज़माएं।

+0

धन्यवाद किसी भी विचार को प्रथम नाम मुद्रित करने के लिए कैसे? या कोई अन्य? – user187580

+0

Iam प्रिंट करने के लिए print_r ($ डेटा) का निरीक्षण कर रहा है जैसे कि यह एक सरणी था .. 'firstname' => firstname value आदि। यह मेरे लिए ठीक काम कर रहा था लेकिन शायद एक्सएमएल डेटा स्रोत पर कुछ बदलावों ने इसे खाली कर दिया। – user187580

+0

मेरा अद्यतन उत्तर देखें। –

5

अच्छा, यह एक खाली वस्तु नहीं है। दरअसल, अगर आप इसे प्रिंट करते हैं तो यह दिखाता है कि आपने हमें क्या दिखाया है। लेकिन अगर उदाहरण के लिए आप

echo $data->asXML(); 

कर परिणाम सही हो जाएगा:

<?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/> 
... 

बस वस्तु का उपयोग के रूप में SimpleXML अगर इसे सही ढंग से भरी हुई

जांच करने के लिए :) करने के लिए है, को देखने के डॉक:

त्रुटियाँ/अपवाद

XML डेटा में मिली प्रत्येक त्रुटि के लिए एक E_WARNING त्रुटि संदेश बनाता है और त्रुटियों को पता चला तो अपवाद फेंकता है।

page

+0

धन्यवाद वास्तव में यह काम करता है :) मैं प्रथम नाम, आखिरी नाम कैसे मुद्रित करूं? – user187580

+0

ठीक है, जैसा कि एएक्स उत्तर में है :) –

0

पर या वर के लिए डंप यह, NS7 नोड से नेमस्पेसिंग हटाने जड़ पर उन्हें छोड़ने:

<?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

धन्यवाद .. किसी भी विचार को प्रथम नाम, अंतिम नाम आदि का उपयोग कैसे करें? – user187580

+0

चूंकि यह एक ऑब्जेक्ट है, आप मानक ऑब्जेक्ट रेफरेंसिंग का उपयोग कर सकते हैं: $ डेटा-> उपयोगकर्ता-> firstName – Ashley

0

हाँ मैं एक ही मुद्दा था और सोचा था कि simplexml_load_string लौट रहा था print_r ($ डेटा) के बाद खाली या $ डेटा गूंजें बस खाली लौटा दिया।

लेकिन यदि आप $ डेटा-> नाम करते हैं तो आपको एक वैध डेटा मिलता है .. यह थोड़े से विचलित है लेकिन यह कैसे काम करता है .. बहुत अच्छी युक्ति .. धन्यवाद ..यह मेरे लिए काम किया

संबंधित मुद्दे