• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

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>
0
pratikroy
Asked:
pratikroy
2 Solutions
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now