2014-03-12 77 views
0

我試圖解析XML,但是當我試圖讓第一線檢索用戶名和密碼無法從XML文檔根節點

我打開我的文檔這樣

$xmlLead = new SimpleXMLElement($sXml); 
$domLead = dom_import_simplexml($xmlLead); 
//this line give me a technical error 
echo $domLead->documentElement; 

這裏是我完整的XML

<Operations username="test" password="test" > 
    <SetRequest> 
     <PersonalData> 
         <CareID/> 
         <LeadID>IT1234</LeadID> 
         <Name>John</Name> 
         <Surname>Doe</Surname> 
         <SecondSurname>Smith</SecondSurname> 
         <Salutation>Mr</Salutation> 
         <Phone>+39011525354</Phone> 
         <Mobile>+393381234567</Mobile> 
         <Email>[email protected]</Email> 
         <Address>Via Po 10</Address> 
         <City>Turin</City> 
         <District>Piedmont</District> 
         <ZipCode>10100</ZipCode> 
         <Country>1000</Country> 
         <Language>5</Language> 
         <Gender>Male</Gender> 
         <BirthPlace /> 
         <BirthDate>1990/02/17 00:00</BirthDate> 
         <PersonalID>KRTCBN65R55G822P</PersonalID> 
         <Job>Workman</Job> 
         <Company>Apple</Company> 
         <CompanyID/> 
         <PurchaseIntention>12+</PurchaseIntention> 
         <OwnedCar brandCode="0F1" modelCode="Y03" brandDescription="CITROEN" modelDescription="CITROEN C4 PICASSO" engineType="petrol" odometer="" registerDate="" chassisNumber="" plateNumber="" price="" carRecovered=""/> 
         <ExtraField id="1" fieldName="POBox">0112947</ExtraField> 
         <ExtraField id="2" fieldName="Second Address">Piazza di Spagna 5, Roma</ExtraField> 
     </PersonalData> 
    <PrivacyData> 
     <SpecificPrivacy>1</SpecificPrivacy> 
     <ExtendedPrivacy> 
      <Privacy channelType="Phone">1</Privacy> 
      <Privacy channelType="Email">0</Privacy> 
      <Privacy channelType="Post">0</Privacy> 
     </ExtendedPrivacy> 
    </PrivacyData> 
    <RequestData> 
       <Channel>Internet</Channel> 
       <Source>Internet Site</Source> 
       <SourceDetail>Form TD</SourceDetail> 
       <SourceCode /> 
       <CampaignCode /> 
       <Level1>WEB CORPORATE</Level1> 
       <Level2>STANDARD</Level2> 
       <Level3 >N_A</Level3 > 
       <Level4>BRAND</Level4> 
       <Campaign>Campaign Test</Campaign > 
       <Offer>Offer Test</Offer > 
       <UsedDevice>Mobile</UsedDevice> 
       <RequestsGroup> </RequestsGroup> 
       <RequestDate>2013/02/27 17:34</RequestDate> 
       <RequestsGroup brand="Fiat" brandID="00"> 
        <Dealer name="SPAZIO S.P.A." sincom="62211" siteCode="123" address="via dei missaglia 89" market="1000" city="torino" province="torino" email="[email protected]" dealerZipCode="10022"></Dealer> 
        <Request type="TD" detail=""> 
          <CarModel code="150" modelDescription="" engineType="petrol"/> 
        </Request> 
       </RequestsGroup> 
    </RequestData> 
</SetRequest> 

感謝您的幫助,因爲我什麼都試過,我t不起作用。 雖然我可以檢索它們的另一個節點

+0

我不是abble檢索拉辛節點「操作」 – user1898765

回答

0

$ domLead已經是documentElement,而不是文檔。您將XML加載到SimpleXMLElement對象中,這將始終表示一個元素,而不是文檔。所以你將文檔元素導入DOM。

$xmlLead = new SimpleXMLElement('<Operations username="test" password="test"/>'); 
$domLead = dom_import_simplexml($xmlLead); 
var_dump(get_class($domLead)); 

輸出:

string(10) "DOMElement" 

如果你想使用DOM,直接創建DOM文檔和您的XML加載到其中:

$dom = new DOMDocument(); 
$dom->loadXml('<Operations username="test" password="test"/>'); 
var_dump(get_class($dom), get_class($dom->documentElement)); 

輸出:

string(11) "DOMDocument" 
string(10) "DOMElement" 

如果你有你的XML加載到一個DOM,你可以創建一個DOMXpath對象並獲取數據:

$dom = new DOMDocument(); 
$dom->loadXml('<Operations username="test" password="test"/>'); 
$xpath = new DOMXpath($dom); 

var_dump(
    $xpath ->evaluate('string(/*/@username)'), 
    $xpath ->evaluate('string(/*/@password)') 
); 

輸出:

string(4) "test" 
string(4) "test" 
+0

thansks了很多,現在它工作正常,並DOMXpath真的很有用 – user1898765