Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    May 2009
    Posts
    165
    Thanks
    77
    Thanked 1 Time in 1 Post

    SimpleXML with Namespaces

    Hi guys, having a bit of a problem. I am trying to evaluate some XML. however I dont want to do this using XPath. I had problems loading the xml, but managed to do so now using simplexml_import_dom. Anyways, the format of the XML is like so (I have not created this, it is returned from a server).
    Code:
    <SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
       <SOAP:Body>
          <hotel:HotelSearchAvailabilityRsp TraceId="test" ResponseTime="28972" xmlns:hotel="..." xmlns:common_v26_0="...">
             <hotel:HotelSearchResult>
                <common_v26_0:VendorLocation VendorCode="MU" VendorLocationID="LONBR" />
                <hotel:HotelProperty HotelChain="MU" HotelCode="BR" HotelLocation="LR" Name="MILLENNIUM" Availability="Available">
                   <hotel:PropertyAddress>
                      <hotel:Address>44 SQUARE</hotel:Address>
                      <hotel:Address>ENGLAND</hotel:Address>
                   </hotel:PropertyAddress>
                   <common_v26_0:PhoneNumber Type="Hotel" Number="029"/>              
                </hotel:HotelProperty>
                <hotel:RateInfo ApproximateMinimumAmount="GBP330.00" ApproximateMaximumAmount="GBP610.00"/>
             </hotel:HotelSearchResult>
          </hotel:HotelSearchAvailabilityRsp>
       </SOAP:Body>
    </SOAP:Envelope>
    How would I print out the Address? The xml is stored in a variable, so this is what I am currently attempting
    Code:
    $dom = new DOMDocument('1.0', 'UTF-8');
    $dom->loadXML($serverResponse);  //xml string
    $sxe = simplexml_import_dom($dom);
    
    foreach ($sxe as $out_ns)
    {
        $ns = $out_ns->getNamespaces(true);
        $child = $out_ns->children($ns['hotel']->address);
    
        foreach ($child as $out)
        {
            echo $out . "<br />";
        }
    }
    Any advice appreciated.

    Cheers

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,519
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    I wanted to take time and create/test a script that I know will work, but I didn't have the time this morning.

    Here's a similar XML parse example that you can try:
    Parse XML with namespace by SimpleXML in PHP | Amigo's Technical Notes

    That might help you figure-out how to "drill-down" the XML to get your hotel address.

    NOTE:
    I guess the main difference is that you would use: simplexml_load_string( );
    Last edited by mlseim; 05-08-2014 at 01:24 PM.

  • Users who have thanked mlseim for this post:

    nick2price (05-08-2014)

  • #3
    Regular Coder
    Join Date
    May 2009
    Posts
    165
    Thanks
    77
    Thanked 1 Time in 1 Post
    I cant use simplexml_load_string() because I dont think my xml is well formatted enough to use this - when I did try using it nothing happened.

    I thought I would try a different approach, using only DOMDocument. Is there a way I can do something like the following?

    Code:
        $dom = new DOMDocument('1.0', 'UTF-8');
        $dom->loadXML($serverResponse);
        foreach ($dom->getElementsByTagNameNS('hotel_v26_0', '*') as $element)
        {
            foreach($element->getElementsByTagName('hotel:HotelProperty') as $hotel)
            {
                echo $hotel->item(0)->nodeValue, PHP_EOL;
            }
        }
    Thanks

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,519
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    This XML stuff is so frustrating ... like there is no "standard" or "well-formed" requirements.
    Everyone just does their own thing.

    Just on a whim, does your hotel system offer any other format, like JSON?

  • #5
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    852
    Thanks
    173
    Thanked 94 Times in 94 Posts
    I came up with this. May or may not be useful.

    How would I print out the Address?

    PHP Code:
    $obj simplexml_load_file('feed.xml');
    $address = [];
    foreach( 
    $obj->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('...')->HotelSearchAvailabilityRsp->HotelSearchResult->HotelProperty->PropertyAddress as $add )
    {
        
    $address $add->Address;
    }

    echo 
    $address[0] . ', ' .$address[1];


    /*output
    44 SQUARE, ENGLAND
    */ 
    Last edited by low tech; 05-09-2014 at 09:51 AM.
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • Users who have thanked low tech for this post:

    nick2price (05-12-2014)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •