Go Back   SitePoint Forums > Forum Index > Program Your Site > PHP > PHP Application Design
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Old Oct 6, 2005, 01:47   #1
lvismer
SitePoint Zealot
 
Join Date: Aug 2005
Location: South Africa
Posts: 185
Creating a DataObject from an XML file

Hi

I have the following XML file that contains a list of HotelDescriptiveContent nodes. I would like to create a DataObject from the XML file. Please assist and see if I am approaching this correctly. Be gentle ;-)

HTML Code:
<?xml version="1.0" encoding="UTF-8"?>
<OTA_HotelDescriptiveInfoRS xmlns="http://www.opentravel.org/OTA/2003/05" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opentravel.org/OTA/2003/05 http://localhost:8080/APIWebModule/FS_OTA_HotelDescriptiveInfoRS.xsd" Version="1.1">
    <Success/>
    <HotelDescriptiveContents>
        <HotelDescriptiveContent HotelCode="ABC" HotelName="ABC Hotel" BrandName="ABC Brand" LanguageCode="">
            <HotelInfo>
                <Descriptions>
                    <Description InfoCode="1">
                        <Text>This is a very beautiful hotel</Text>
                        <Image>http://localhost/images/properties/ABC/ABC.jpg</Image>
                    </Description>
                    <Description InfoCode="3">
                        <Text>This is a beautiful hotel</Text>
                    </Description>
                </Descriptions>
            </HotelInfo>
            <FacilityInfo LastUpdated="2005-05-01T12:00:00">
                <GuestRooms>
                    <GuestRoom Code="B1K" RoomTypeName="Room type name" CodeContext="AB1" MaxOccupancy="10" MaxAdultOccupancy="5" NonsmokingQuantity="0" >
                        <TypeRoom Name="Category name"/>
                        <Description>
                            <Text>This is a beautiful room with a view</Text>
                            <Image>http://localhost/images/properties/ABC/roomtypes/B1K.jpg</Image>
                        </Description>
                    </GuestRoom>
                    <GuestRoom Code="S1K" RoomTypeName="Room type name" >
                        <Description>
                            <Text>This is another beautiful room with a view</Text>
                            <Image>http://localhost/images/properties/ABC/roomtypes/S1K.jpg</Image>
                        </Description>
                    </GuestRoom>
                </GuestRooms>
            </FacilityInfo>
            <AffiliationInfo LastUpdated="2005-05-01T12:00:00">
                <Awards>
                    <Award Rating="5*" />
                </Awards>
            </AffiliationInfo>
            <MultimediaObjects LastUpdated="2005-05-01T12:00:00">
                <MultimediaObject Version="" ContentTitle="" ContentCode="6" ContentData="">
                    <Image>http://localhost/images/properties/ABC/Map.jpg</Image>
                </MultimediaObject>
            </MultimediaObjects>
            <ContactInfos>
                <ContactInfo>
                    <Addresses>
                        <Address Type="7">
                            <AddressLine>11A De La Rey Rood</AddressLine>
                            <AddressLine>Montwood House</AddressLine>
                            <CityName>Rivonia</CityName>
                            <PostalCode>2128</PostalCode>
                            <County>JHB South</County>
                            <StateProv>Gauteng</StateProv>
                            <CountryName>South Africa</CountryName>
                        </Address>
                        <Address Type="2">
                            <AddressLine>PO Box 12345</AddressLine>
                            <AddressLine>Rivonia</AddressLine>
                            <PostalCode>2128</PostalCode>
                        </Address>
                    </Addresses>
                    <Phones>
                        <Phone PhoneTechType="1" PhoneNumber="0118032208"/>
                        <Phone PhoneTechType="3" PhoneNumber="0118071901"/>
                    </Phones>
                    <Emails>
                        <Email>koos@property.com</Email>
                    </Emails>
                    <URLs>
                        <URL>www.property.com</URL>
                    </URLs>
                </ContactInfo>
            </ContactInfos>
            <TPA_Extensions>
                <HotelLocation>
                    <Text>This is the location of the hotel</Text>
                </HotelLocation>
            </TPA_Extensions>
        </HotelDescriptiveContent>
    </HotelDescriptiveContents>
</OTA_HotelDescriptiveInfoRS>
Skeleton code to start with ...

PHP Code:

class OTA_HotelCollection

{
    
public $hotels = array();

    function
addHotel(OTA_Hotel $hotel)
    {
        
$this->hotels[] = $hotel;
    }

    function
nextHotel()
    {
        
$tmp = each($this->hotels);
        return
$tmp['value'];
    }
}

class
OTA_Hotel
{
    
public $name;
    
public $code;
    
public $hotelInfo = array();
    
public $facilityInfo = array();

    function
setHotelInfo(OTA_Description $description)
    {
        
$this->hotelInfo[] = $description;
    }

    function
setName($name)
    {
        
$this->name = $name;
    }

    function
setCode($code)
    {
        
$this->code = $code;
    }

    function
setRoomFacilityInfo(OTA_Facility $description)
    {
        
$this->hotelInfo[] = $description;
    }
}

class
OTA_Description
{
    
public function __construct($infoCode, $text, $image)
    {
        
$this->infoCode = $infoCode;
        
$this->text = $text;
        
$this->image = $image;
    }
}

$hotels = new OTA_HotelCollection();
$hotel1 = new OTA_Hotel();
$hotel1->setName('Hotel 1');
$hotel1->setCode('ABC');
$hotel1->setHotelInfo(new OTA_Description('1', 'hotel 1 info', 'image1.jpg'));
$hotel1->setHotelInfo(new OTA_Description('3', 'hotel 1 info type 3', ''));

$hotel2 = new OTA_Hotel();
$hotel2->setName('Hotel 2');
$hotel2->setCode('DEF');
$hotel2->setHotelInfo(new OTA_Description('1', 'hotel 2 info', 'image2.jpg'));

$hotels->addHotel($hotel1);
$hotels->addHotel($hotel2);

while (
$hotel = $hotels->nextHotel() ) {
    
print_r($hotel);
}
Now to build the DataObject from the xml file. This feels really messy, please advice on a better approach.

PHP Code:

$doc = new DOMDocument('1.0', 'utf-8');

$doc->load('response2.xml');

$hotels = new OTA_HotelCollection();
$hotelDescriptiveContentNodes = $doc->getElementsByTagName('HotelDescriptiveContent');
foreach (
$hotelDescriptiveContentNodes as $hotelDescriptiveContent ) {
    if (
$hotelDescriptiveContent instanceof DOMElement ) {
        
$hotelName = $hotelDescriptiveContent->getAttribute('HotelName');
        
$hotelCode = $hotelDescriptiveContent->getAttribute('HotelCode');
        
$hotel = new OTA_Hotel();
        
$hotel->setName($hotelName);
        
$hotel->setCode($hotelCode);

        
/*
        * Work with the HotelInfo nodes
        */
        
$hotelInfoNodes = $hotelDescriptiveContent->getElementsByTagName('HotelInfo');
        foreach (
$hotelInfoNodes as $hotelInfo ) {
            
$descriptionNodes = $hotelInfo->getElementsByTagName('Description');
            foreach (
$descriptionNodes as $description ) {
                
$infoCode = $description->getAttribute('InfoCode');
                
$childNodes = $description->childNodes;
                foreach (
$childNodes as $node ) {
                    if (
$node->nodeName == 'Text' ) {
                        
$text = $node->nodeValue;
                    } elseif (
$node->nodeName == 'Image' ) {
                        
$image = $node->nodeValue;
                    }
                }
                
$hotel->setHotelInfo(new OTA_Description($infoCode, $text, $image));
            }
        }

        
$hotels->addHotel($hotel);
}
In the OTA_Hotel object, should one use setters of rather use __set? What is the best way to make the XML parsing a little cleaner.

Many thanks
--
lv
lvismer is offline   Reply With Quote
 

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 02:41.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved