我尝试解析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@email.com</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="mydealer@dealer.com" dealerZipCode="10022"></Dealer>
<Request type="TD" detail="">
<CarModel code="150" modelDescription="" engineType="petrol"/>
</Request>
</RequestsGroup>
</RequestData>
</SetRequest>
感谢您的帮助,因为我尝试了一切并且它不起作用。 而另一个节点我可以检索它们
答案 0 :(得分: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,请直接创建DOMDocument并将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"