Jump to content

PHP XML Dom


Cooper94

Recommended Posts

<?php
$dom = new DOMDocument("1.0");
header("Content-Type: text/plain");

$root = $dom->createElement("flights"); 
$dom->appendChild($root);

$item = $dom->createElement("Departure");
$root->appendChild($item);

$text = $dom->createTextNode("KLGA");
$item->appendChild($text);

$item = $dom->createElement("Arrival");
$root->appendChild($item);

$text = $dom->createTextNode("KPHL");
$item->appendChild($text);

$item = $dom->createElement("Aircraft");
$root->appendChild($item);

$text = $dom->createTextNode("E170");
$item->appendChild($text);
echo $dom->saveXML();
?>

 

Is there anyway to create more than one element? Like say I want another element named Flights but just with different information in the nodes with that be possible? Thank you all for your time!

Link to comment
Share on other sites

Ok is there anyway you could help me with this? I am grabing this information from a database and so far I only have this. I thank you again for all the help!

 

<?php
include '../includes/db.php';
$result = mysql_query("SELECT * FROM reports WHERE callsign = '$_GET[pilot]' AND status = '0'");
while ($row = mysql_fetch_array($result)) {
}

$dom = new DOMDocument("1.0");
header("Content-Type: text/plain");

$root = $dom->createElement("flights"); 
$dom->appendChild($root);


$item = $dom->createElement("Departure");
$root->appendChild($item);


$text = $dom->createTextNode("KLGA");
$item->appendChild($text);


$item = $dom->createElement("Arrival");
$root->appendChild($item);


$text = $dom->createTextNode("KPHL");
$item->appendChild($text);


$item = $dom->createElement("Aircraft");
$root->appendChild($item);


$text = $dom->createTextNode("E170");
$item->appendChild($text);

echo $dom->saveXML();
?>

Link to comment
Share on other sites

Sure :)

 

The steps you want to take are

 

#1 - Retrieve your info from the database, and store the results in an array.  Change

<?php
// Not sure what you were trying to do here...gotta do something in the loop 
while ($row = mysql_fetch_array($result)) {
}
?>

 

 

To

while ($row = mysql_fetch_array($result)) {
$data[] = $row;
}

 

Now that we have all of your data in an array, you can then more on to the XML portion.

// I've never used this particular extension..if this doesn't work I'll look into it a bit more.

 

<?php
foreach ($data as $flights){

$root = $dom->createElement("flights"); 
$dom->appendChild($root);


$item = $dom->createElement("Departure");
$root->appendChild($item);


$text = $dom->createTextNode($flight['departure']);
$item->appendChild($text);


$item = $dom->createElement("Arrival");
$root->appendChild($item);


$text = $dom->createTextNode($flight['arrival'];
$item->appendChild($text);


$item = $dom->createElement("Aircraft");
$root->appendChild($item);


$text = $dom->createTextNode($flight['aircraft']);
$item->appendChild($text);

endforeach;?>

 

 

 

 

 

Link to comment
Share on other sites

Lol its alot but I went through it and it is all right! Thank you  :confused:

 

array(2) { [0]=> array(40) { [0]=> string(2) "15" ["id"]=> string(2) "15" [1]=> string(0) "" ["date"]=> string(0) "" [2]=> string(7) "RPA1001" ["callsign"]=> string(7) "RPA1001" [3]=> string(7) "RPA3265" ["flight_num"]=> string(7) "RPA3265" [4]=> string(4) "E170" ["aircraft"]=> string(4) "E170" [5]=> string(0) "" ["online"]=> string(0) "" [6]=> string(0) "" ["duration"]=> string(0) "" [7]=> string(4) "KPHL" ["pilot_hub"]=> string(4) "KPHL" [8]=> string(0) "" ["fuel_used"]=> string(0) "" [9]=> string(0) "" ["pax"]=> string(0) "" [10]=> string(0) "" ["money"]=> string(0) "" [11]=> string(0) "" ["rate"]=> string(0) "" [12]=> string(0) "" ["method"]=> string(0) "" [13]=> string(4) "KALB" ["dep_icao"]=> string(4) "KALB" [14]=> string(0) "" ["dep_time"]=> string(0) "" [15]=> string(4) "KCLT" ["arr_icao"]=> string(4) "KCLT" [16]=> string(0) "" ["arr_time"]=> string(0) "" [17]=> string(26) "PWL CMK J75 GVE LYH SUDSY3" ["route"]=> string(26) "PWL CMK J75 GVE LYH SUDSY3" [18]=> string(1) "0" ["status"]=> string(1) "0" [19]=> string(0) "" ["comments"]=> string(0) "" } [1]=> array(40) { [0]=> string(2) "14" ["id"]=> string(2) "14" [1]=> string(0) "" ["date"]=> string(0) "" [2]=> string(7) "RPA1001" ["callsign"]=> string(7) "RPA1001" [3]=> string(7) "RPA3049" ["flight_num"]=> string(7) "RPA3049" [4]=> string(4) "E145" ["aircraft"]=> string(4) "E145" [5]=> string(0) "" ["online"]=> string(0) "" [6]=> string(0) "" ["duration"]=> string(0) "" [7]=> string(4) "KPHL" ["pilot_hub"]=> string(4) "KPHL" [8]=> string(0) "" ["fuel_used"]=> string(0) "" [9]=> string(0) "" ["pax"]=> string(0) "" [10]=> string(0) "" ["money"]=> string(0) "" [11]=> string(0) "" ["rate"]=> string(0) "" [12]=> string(0) "" ["method"]=> string(0) "" [13]=> string(4) "KCLT" ["dep_icao"]=> string(4) "KCLT" [14]=> string(0) "" ["dep_time"]=> string(0) "" [15]=> string(4) "KSDF" ["arr_icao"]=> string(4) "KSDF" [16]=> string(0) "" ["arr_time"]=> string(0) "" [17]=> string(0) "" ["route"]=> string(0) "" [18]=> string(1) "0" ["status"]=> string(1) "0" [19]=> string(0) "" ["comments"]=> string(0) "" } } 

Link to comment
Share on other sites

Ok I got it to display something but it dosnt display all of it, it gives me this error:

The XML page cannot be displayed

Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.

Only one top level element is allowed in an XML document. Error processing resource 'http://www.purified-designs.com/V2/aca...

<flights><Departure></Departure><Arrival>KSDF</Arrival><Aircraft>E145</Aircraft>&l...

 

Here is the code!Thank you!

 

<?php
include '../includes/db.php';
$result = mysql_query("SELECT * FROM reports WHERE callsign = '$_GET[pilot]' AND status = '0'");
$num = mysql_num_rows($result);
while ($row = mysql_fetch_array($result)){

$data[] = $row;

}

$dom = new DOMDocument("1.0");
header("Content-Type: text/plain");

foreach ($data as $flights){

$root = $dom->createElement("flights"); 
$dom->appendChild($root);


$item = $dom->createElement("Departure");
$root->appendChild($item);


$text = $dom->createTextNode($flight['dep_icao']);
$item->appendChild($text);


$item = $dom->createElement("Arrival");
$root->appendChild($item);


$text = $dom->createTextNode($flights['arr_icao']);
$item->appendChild($text);


$item = $dom->createElement("Aircraft");
$root->appendChild($item);


$text = $dom->createTextNode($flights['aircraft']);
$item->appendChild($text);
}

echo $dom->saveXML();
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.