#1
  1. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,015
    Rep Power
    175

    Problem with parsing xml with php


    Hello;

    I receive an xml from a website (example below) and based on the HotelCode I generate another xml and send it back to them. At this stage I have problems parsing their xml. What am I doing wrong? Thanks.
    PHP Code:
    $xml simplexml_load_file("receive.xml");
    echo 
    $xml->getName() . "<br />";
    foreach(
    $xml->children() as $child)
      {
             echo 
    $child->getName() . ": " $child "<br />";
      } 
    receive.xml
    Code:
    <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
      <env:Header>
        <wsse:Security soap:mustUnderstand='1' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:wsse='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'>
          <wsse:UsernameToken>
            <wsse:Username>Username</wsse:Username>
            <wsse:Password Type='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText'>Password</wsse:Password>
          </wsse:UsernameToken>
        </wsse:Security>
      </env:Header>
      <env:Body>
        <OTA_HotelAvailRQ Version='1.0' TimeStamp='2012-07-20T09:41:02+10:00' EchoToken='abc123' AvailRatesOnly='true' xmlns='http://www.opentravel.org/OTA/2003/05'>
          <AvailRequestSegments>
            <AvailRequestSegment AvailReqType='Room'>
              <HotelSearchCriteria>
                <Criterion>
                  <HotelRef HotelCode='1961'/>
                </Criterion>
              </HotelSearchCriteria>
            </AvailRequestSegment>
          </AvailRequestSegments>
        </OTA_HotelAvailRQ>
      </env:Body>
    </env:Envelope>
    Results so far:
    Code:
    Envelope<br />
    PHP Code:
    print_r($xml); 
    Code:
    SimpleXMLElement Object
    (
    )
    Last edited by zxcvbnm; July 23rd, 2012 at 09:43 PM.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1016
    Hi,

    You have to provide the URI of the "env" namespace:

    PHP Code:
    foreach($xml->children("http://schemas.xmlsoap.org/soap/envelope/") as $child
  4. #3
  5. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,015
    Rep Power
    175
    Originally Posted by Jacques1
    Hi,

    You have to provide the URI of the "env" namespace:

    PHP Code:
    foreach($xml->children("http://schemas.xmlsoap.org/soap/envelope/") as $child
    Helo; thank you for the reply. I tried this but I still get nothing.
    PHP Code:
    $xml simplexml_load_file("receive.xml");
    echo 
    $xml->getName() . "<br />";
    foreach(
    $xml->children("http://schemas.xmlsoap.org/soap/envelope/") as $child
        {
            echo 
    $child;
        } 
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1016
    What do you mean by "nothing"? You get two elements, env:Header and env:Body. Replace "echo $child" with "var_dump($child)", then you should see the children.

    I don't know what you want, so it's kind of hard to help.
  8. #5
  9. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,015
    Rep Power
    175
    Originally Posted by Jacques1
    What do you mean by "nothing"? You get two elements, env:Header and env:Body. Replace "echo $child" with "var_dump($child)", then you should see the children.

    I don't know what you want, so it's kind of hard to help.
    Hey. No its not working. This is what I get!
    Code:
    object(SimpleXMLElement)#4 (0) {
    }
    object(SimpleXMLElement)#5 (0) {
    }
    Last edited by zxcvbnm; July 24th, 2012 at 12:07 AM.
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1016
    The children of the children are in different namespaces, so the elements appear to be empty. But they're not, you just have to provide the correct namespace for the children again.

    To get all children regardless of the namespace, use XPath:
    PHP Code:
    $xml simplexml_load_file("receive.xml"); 
    echo 
    $xml->getName() . "<br />";
    foreach (
    $xml->xpath('*') as $child) {
        echo 
    $child->getName() . '<br />';

  12. #7
  13. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,015
    Rep Power
    175
    Originally Posted by Jacques1
    The children of the children are in different namespaces, so the elements appear to be empty. But they're not, you just have to provide the correct namespace for the children again.

    To get all children regardless of the namespace, use XPath:
    PHP Code:
    $xml simplexml_load_file("receive.xml"); 
    echo 
    $xml->getName() . "<br />";
    foreach (
    $xml->xpath('*') as $child) {
        echo 
    $child->getName() . '<br />';

    Hey thank you. I am still trying to find my way to HotelCode='1961' . How can I dig down to that?
  14. #8
  15. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1016
    Why didn't you say that earlier? Then we could have skipped all that children stuff, because it's completely worthless for your task.

    PHP Code:
    $hotel_ref $xml->xpath("//*[local-name()='HotelRef']");
    $attributes $hotel_ref[0]->attributes();
    echo 
    $attributes['HotelCode']; 

    Comments on this post

    • English Breakfast Tea agrees : Thanks man its working nicely :)
  16. #9
  17. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,015
    Rep Power
    175
    Originally Posted by Jacques1
    Why didn't you say that earlier? Then we could have skipped all that children stuff, because it's completely worthless for your task.
    PHP Code:
    $xml->registerXPathNamespace('ot''http://www.opentravel.org/OTA/2003/05');
    $hotel_ref $xml->xpath("//ot:HotelRef");
    $attributes $hotel_ref[0]->attributes();
    echo 
    $attributes['HotelCode']; 
    Oh mate, thank you for all your support.

IMN logo majestic logo threadwatch logo seochat tools logo