質問

I'm trying to parse an xml but when i try to get the first line to retrieve the username and password

I load my document like this

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

Here is my complete 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>

thanks for your help because i tried everything and it doesn't work. While the other node i can retrieve them

役に立ちましたか?

解決

The $domLead already is the documentElement, not the document. You're loading the XML into a SimpleXMLElement object, this will always represent an element, never a document. So you're importing the document element into DOM.

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

Output:

string(10) "DOMElement"

If you would like to use DOM, create the DOMDocument directly and load your XML into it:

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

Output:

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

If you have your XML loaded into a DOM, you can create an DOMXpath object and fetch data:

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

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

Output:

string(4) "test"
string(4) "test"
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top