Read node from xml file

hi, ihave somme deffecult with XML interpretation, as you can see my error code is

Fatal error: Uncaught exception ‘Exception’ with message ‘String could not be parsed as XML’ in C:… SimpleXMLElement->__construct(’’) #1 {main} thrown in C:…; when i tried to read child from xml:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:con="http://ericsson.com/services/ws_cma3/contractssearch">
   <soapenv:Header/>
   <soapenv:Body>
      <con:contractsSearchResponse>
         <!--You may enter the following 2 items in any order-->
         <!--Optional:-->
         <con:contracts>
            <!--Zero or more repetitions:-->
            <con:item>
               <!--You may enter the following 39 items in any order-->
               <!--Optional:-->
               <con:contractTypeId>fysal</con:contractTypeId>
               <!--Optional:-->
               <con:foreignPlcode>123</con:foreignPlcode>
               <!--Optional:-->
               <con:homeBidGroupId>?</con:homeBidGroupId>
               <!--Optional:-->
               <con:foreignBidGroupId>?</con:foreignBidGroupId>
               <!--Optional:-->
               <con:coLec>?</con:coLec>
               <!--Optional:-->
               <con:buId>?</con:buId>
               <!--Optional:-->
               <con:coStatus>?</con:coStatus>
               <!--Optional:-->
               <con:csCode>?</con:csCode>
               <!--Optional:-->
               <con:dirnum>?</con:dirnum>
               <!--Optional:-->
               <con:dirnumBlocks>
                  <!--Zero or more repetitions:-->
                  <con:item>
                     <!--You may enter the following 2 items in any order-->
                     <!--Optional:-->
                     <con:lowerExt>?</con:lowerExt>
                     <!--Optional:-->
                     <con:upperExt>?</con:upperExt>
                  </con:item>
               </con:dirnumBlocks>
               <!--Optional:-->
               <con:adrFname>?</con:adrFname>
               <!--Optional:-->
               <con:adrLname>?</con:adrLname>
               <!--Optional:-->
               <con:adrName>?</con:adrName>
               <!--Optional:-->
               <con:adrStreet>?</con:adrStreet>
               <!--Optional:-->
               <con:adrStreetno>?</con:adrStreetno>
               <con:markedForRerating>?</con:markedForRerating>
               <!--Optional:-->
               <con:csDealerid>?</con:csDealerid>
               <!--Optional:-->
               <con:sharedBusinessStartDate>?</con:sharedBusinessStartDate>
               <!--Optional:-->
               <con:sharedBusinessExpirationDate>?</con:sharedBusinessExpirationDate>
            </con:item>
         </con:contracts>
         <!--Optional:-->
         <con:searchIsComplete>?</con:searchIsComplete>
      </con:contractsSearchResponse>
   </soapenv:Body>
</soapenv:Envelope>

my PHP code is:

  $reponse= $soapClient->__getLastResponse();
      $xml = new SimpleXMLElement($reponse);
      /* Search for <a>
                     <b> 
                       <c> */

      $result = $xml->xpath('contractsSearchResponse/contracts/item/contractTypeId');

      while(list( , $node) = each($result)) 
      {
        echo '/contractsSearchResponse/contracts/item/contractTypeId: ',$node,"\n";
       }

Do you get the same error if you use the simplexml_load_string() function?

$xml = simplexml_load_string($response);

The other part of your problem is that the soap body has namespaces in it, which you’ll need to register first.

So

becomes something like

$xml->registerXPathNamespace('s', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('c', 'http://ericsson.com/services/ws_cma3/contractssearch');

$result = $xml->xpath('//s:Body/c:contractsSearchResponse/c:contracts/c:item/c:contractTypeId');

i do exacltly what yo expalin me, but i still have probleme:

Fatal error: Class ‘simplexml_load_string’ not found in C:\EasyPHP 2.0b1\www\New_proget_finale_ayoub\webservice4glamya\testayoub\tesd.php on line 59

i modify my code to:

$reponse=$soapClient->__doRequest($message, 'http://DELL:8088/mockContractsSearchServiceSoap11', 'create', 1);

 echo $soapClient->__getLastRequest();
apClient->__getLastRequest();
      
      $reponse= $soapClient->__getLastResponse();
      $xml = new simplexml_load_string($reponse);
      /* Search for <a>
                     <b> 
                       <c> */

      $xml->registerXPathNamespace('s', 'http://schemas.xmlsoap.org/soap/envelope/');
      $xml->registerXPathNamespace('c', 'http://ericsson.com/services/ws_cma3/contractssearch');
      $result = $xml->xpath('//s:Body/c:contractsSearchResponse/c:contracts/c:item/c:contractTypeId');
      while(list( , $node) = each($result)) 
      {
        echo '/contractsSearchResponse/contracts/item/contractTypeId: ',$node,"\n";
       }

echo "Response:\n" . $soapClient->__getLastResponse() . "\n";//var_dump ($reponse);
var_dump ($reponse);

tks a lot for your ansert

It’s a function, not a class :wink:

If you remove the ‘new’ keyword, what happens?

i show this mesage

Fatal error:  Call to a member function registerXPathNamespace() on a non-object in C:\EasyPHP 2.0b1\www\New_proget_finale_ayoub\webservice4glamya\testayoub\tesd.php on line 65

Then the value of $reponse isn’t valid xml. move the var_dump to the line below where $reponse is set and post the value here?

this is my update code suche as what you say to me:
echo $soapClient->__getLastRequest();
var_dump ($reponse);
$reponse= $soapClient->__getLastResponse();
$xml = simplexml_load_string($reponse);
print_r($xml);
/* Search for

*/

      $xml->registerXPathNamespace('s', 'http://schemas.xmlsoap.org/soap/envelope/');
      $xml->registerXPathNamespace('c', 'http://ericsson.com/services/ws_cma3/contractssearch');
      $result = $xml->xpath('//s:Body/c:contractsSearchResponse/c:contracts/c:item/c:contractTypeId');
      while(list( , $node) = each($result)) 
      {
        echo '/contractsSearchResponse/contracts/item/contractTypeId: ',$node,"\n";
       }

i have the same error:

Fatal error: Call to a member function registerXPathNamespace() on a non-object in C:\EasyPHP 2.0b1\www\New_proget_finale_ayoub\webservice4glamya\testayoub\tesd.php on line 66

finaly i resolve the erroe by delete the line $reponse= $soapClient->__getLastResponse();
i have a result:

SimpleXMLElement Object ( ) /contractsSearchResponse/contracts/item/contractTypeId: fysal

the previus message show the resullt :smile: fysal
but he is not the only elemente of my response it should show me:

Response: string(4220) " fysal 123 ? ? ? ? ? "

i wich get all of my body node in forme
$contractTypeId= fysal
$foreignplcode=123
$homebidgroupid=?
…=…
bdy node:

<soapenv:body>
      <con:contractssearchresponse>
         <!--You may enter the following 2 items in any order-->
         <!--Optional:-->
         <con:contracts>
            <!--Zero or more repetitions:-->
            <con:item>
               <!--You may enter the following 39 items in any order-->
               <!--Optional:-->
               <con:contracttypeid>fysal</con:contracttypeid>
               <!--Optional:-->
               <con:foreignplcode>123</con:foreignplcode>
               <!--Optional:-->
               <con:homebidgroupid>?</con:homebidgroupid>
               <!--Optional:-->
               <con:foreignbidgroupid>?</con:foreignbidgroupid>
               <!--Optional:-->
               <con:colec>?</con:colec>
               <!--Optional:-->
               <con:buid>?</con:buid>
               <!--Optional:-->
               <con:costatus>?</con:costatus>
               <!--Optional:-->
               <con:cscode>?</con:cscode>
               <!--Optional:-->
               <con:dirnum>?</con:dirnum>
               <!--Optional:-->
               <con:dirnumblocks>
                  <!--Zero or more repetitions:-->
                  <con:item>
                     <!--You may enter the following 2 items in any order-->
                     <!--Optional:-->
                     <con:lowerext>?</con:lowerext>
                     <!--Optional:-->
                     <con:upperext>?</con:upperext>
                  </con:item>
               </con:dirnumblocks>
               <!--Optional:-->
               <con:adrfname>?</con:adrfname>
               <!--Optional:-->
               <con:adrlname>?</con:adrlname>
               <!--Optional:-->
               <con:adrname>?</con:adrname>
               <!--Optional:-->
               <con:adrstreet>?</con:adrstreet>
               <!--Optional:-->
               <con:adrstreetno>?</con:adrstreetno>
               <!--Optional:-->
               <con:adrzip>?</con:adrzip>
               <!--Optional:-->
               <con:adrcity>?</con:adrcity>
               <!--Optional:-->
               <con:submid>?</con:submid>
               <!--Optional:-->
               <con:plcode>?</con:plcode>
               <!--Optional:-->
               <con:cotype>?</con:cotype>
               <!--Optional:-->
               <con:vpnid>?</con:vpnid>
               <!--Optional:-->
               <con:externalind>?</con:externalind>
               <!--Optional:-->
               <con:rpcode>?</con:rpcode>
               <!--Optional:-->
               <con:coactivated>?</con:coactivated>
               <!--Optional:-->
               <con:portnum>?</con:portnum>
               <!--Optional:-->
               <con:smnum>?</con:smnum>
               <!--Optional:-->
               <con:devportnum>?</con:devportnum>
               <!--Optional:-->
               <con:coid>?</con:coid>
               <!--Optional:-->
               <con:coidpub>?</con:coidpub>
               <!--Optional:-->
               <con:maincoid>?</con:maincoid>
               <!--Optional:-->
               <con:maincoidpub>?</con:maincoidpub>
               <!--Optional:-->
               <con:csid>?</con:csid>
               <!--Optional:-->
               <con:csidpub>?</con:csidpub>
               <!--Optional:-->
               <con:description>?</con:description>
               <!--Optional:-->
               <con:currentdn>?</con:currentdn>
               <!--Optional:-->
               <con:markedforrerating>?</con:markedforrerating>
               <!--Optional:-->
               <con:csdealerid>?</con:csdealerid>
               <!--Optional:-->
               <con:sharedbusinessstartdate>?</con:sharedbusinessstartdate>
               <!--Optional:-->
               <con:sharedbusinessexpirationdate>?</con:sharedbusinessexpirationdate>
            </con:item>
         </con:contracts>
         <!--Optional:-->
         <con:searchiscomplete>?</con:searchiscomplete>
      </con:contractssearchresponse>
   </soapenv:body>

Your xpath search pattern only looks for contractTypeId so only ‘fysal’ is correct given the xml being searched.

Change it to

$result = $xml->xpath('//s:Body/c:contractsSearchResponse/c:contracts/c:item');

And $return should give you the con:item nodes, which you will then need to iterate over to get the node values inside each ‘item’.

My freind, if i put your code like:

$xml->registerXPathNamespace(‘s’, ‘http://schemas.xmlsoap.org/soap/envelope/’);
$xml->registerXPathNamespace(‘c’, ‘http://ericsson.com/services/ws_cma3/contractssearch’);
$result = $xml->xpath(’//s:Body/c:contractsSearchResponse/c:contracts/c:item’);
while(list( , $node) = each($result))
{
echo '/contractsSearchResponse/contracts/item/contractTypeId: ',$node,"\n";
}
I have any result result, purhaps this probleme is due to children of item node(and tehre are 2 same item node) see these lines

<con:item>
               <!--You may enter the following 39 items in any order-->
               <!--Optional:-->
               <con:contracttypeid>fysal</con:contracttypeid>
               <!--Optional:-->
               <con:foreignplcode>123</con:foreignplcode>
               <!--Optional:-->
               <con:homebidgroupid>?</con:homebidgroupid>
               <!--Optional:-->
               <con:foreignbidgroupid>?</con:foreignbidgroupid>
               <!--Optional:-->
               <con:colec>?</con:colec>
               <!--Optional:-->
               <con:buid>?</con:buid>
               <!--Optional:-->
               <con:costatus>?</con:costatus>
               <!--Optional:-->
               <con:cscode>?</con:cscode>
               <!--Optional:-->
               <con:dirnum>?</con:dirnum>
               <!--Optional:-->
               <con:dirnumblocks>
                  <!--Zero or more repetitions:-->
                  <con:item>
                     <!--You may enter the following 2 items in any order-->
                     <!--Optional:-->
                     <con:lowerext>?</con:lowerext>
                     <!--Optional:-->
                     <con:upperext>?</con:upperext>
                  </con:item>
               </con:dirnumblocks>

i apresiate your effort my freind.

Yes, as I said, once you’ve found the item nodes with the xpath query, you’d need to iterate over the child nodes within each ‘item’ to collect the data that you want from that item.

eg.

while(list(, $node) = each ($result)) {
  foreach ($node->children('con', true) as $contractDetail) {
    echo "\n tag: ", $contractDetail->getName(), " value: ", $contractDetail;
  }
}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.