Create XML via PHP - Help needed urgently

Hi friends,

I have been trying various functions of DOMDocument. I have managed to create a simple XML, but I need to create an XML in the following format. I think I would be able to use the relevant variables for replacing values. Can someone help me create this as a static XML?

<BookedSeatCountMsg xsi:schemaLocation="http://abc.com/apd/manager/planning BookedSeatCount.xsd" xmlns="http://abc.com/apd/manager/planning" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <FlightID>
            <FlightDesc>
                  <Airline>CA</Airline>
                  <FlightNum>0606</FlightNum>
            </FlightDesc>
            <LegDepartureDate>2010-04-19T10:10:00+03:00</LegDepartureDate>
            <Origin>DEL</Origin>
            <UniqueID>
                  <TodClock>14171152328894779264</TodClock>
                  <PssCpuID>A</PssCpuID>
            </UniqueID>
      </FlightID>
      <WeightUnits>Kilograms</WeightUnits>
      <Status>PD</Status>
      <BookedSeatCount classOfService="J">
            <Destination>MBA</Destination>
            <PsgrsCount>
                  <PsgrInfo passengerType="M">
                        <ThruOrLocal>Local</ThruOrLocal>
                        <PadOrPax>PAX</PadOrPax>
                  </PsgrInfo>
                  <Count>3</Count>
            </PsgrsCount>            
      </BookedSeatCount>
      <BookedSeatCount classOfService="M">
                  <Destination>MBA</Destination>
                  <PsgrsCount>
                        <PsgrInfo passengerType="A">
                              <ThruOrLocal>Local</ThruOrLocal>
                              <PadOrPax>PAX</PadOrPax>
                        </PsgrInfo>
                        <Count>61</Count>
                  </PsgrsCount>
      </BookedSeatCount>
</BookedSeatCountMsg>
LVL 9
pratikroyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Beverley PortlockCommented:
You say that the XML is to be static. In that case mark the values you wish to replace with some easily spotted text and use str_replace to swap them for the actual values. The code fragment below illustrated the sort of thing I'm talking about although I have only done part of it.

Also, see http://www.php.net/str_replace for more info. To Dynamically generate XML then PHP's SimpleXML would probably do the job. http://www.php.net/simplexml


<?php

$xml = '<BookedSeatCountMsg xsi:schemaLocation="http://abc.com/apd/manager/planning BookedSeatCount.xsd" xmlns="http://abc.com/apd/manager/planning" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <FlightID>
            <FlightDesc>
                  <Airline>AIRLINE</Airline>
                  <FlightNum>FLIGHT_NUM</FlightNum>
            </FlightDesc>
            <LegDepartureDate>DEP_DATE</LegDepartureDate>
            <Origin>ORIGIN</Origin>
            <UniqueID>
                  <TodClock>TOD_CLOCK</TodClock>
                  <PssCpuID>PSS_CPU_ID</PssCpuID>

  .... etc etc
';

// Get PHP variables
//
$airline = ....
$flightNum = .....

$newXml = str_replace( array("AIRLINE", "FLIGHT_NUM", "DEP_DATE", "ORIGIN", ....etc etc ),
                       array($airline,  $flightNum,   $depDate,   $origin ),
                       $xml
                     );


?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.