[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Create an XML file in php

Posted on 2007-08-02
11
Medium Priority
?
344 Views
Last Modified: 2013-12-12
Hi,
I'm trying to pull data out of a SQL Server database in php and I would like to output it in XML format. Can someone briefly show me a simple example? I'm using php 5
0
Comment
Question by:alateos
  • 4
  • 4
  • 3
11 Comments
 
LVL 16

Expert Comment

by:BongSoo
ID: 19617964
Here is an example of one of my scripts for a simple blog I wrote a long time ago, but it should help. It works with an existing XML file for an RSS feed, but you should be able to modify it to create a new one if necessary. Note my extensive use of the error message... LOL!

<?php
// Query database to get all blogs in descending order
$res_allblogs2 = mysql_query("SELECT blogID, DATE_FORMAT((date+interval 2 hour), '%M%d%Y%h%i%p') AS date, title, author, description, body FROM zenblog ORDER BY blogID DESC LIMIT 1") or die("SQL error: " . mysql_error());


//Open the XML file to  write it.
      $fpx = fopen("zenblog.xml", 'wb', true) or die("Error opening the file to write");
      
// write XML header
      fwrite($fpx, "<?xml version=\"1.0\"?> \n") or die("Error writing XML header");
      fwrite($fpx, "<rss version=\"2.0\"> \n") or die("Error writing the rss version");
      fwrite($fpx, "<channel> \n") or die("Error writing channel");
      fwrite($fpx, "<title>WorldZen.org</title> \n") or die("Error writing the title");
      fwrite($fpx, "<link>http://www.worldzen.org/</link> \n") or die("Error writing the main link");
      fwrite($fpx, "<description>The World Zen Fellowship has teaching centers in the Washington DC metropolitan area (The Potomac Zen Sangha), New York City, (Patriarchal Zen Society), Denver, Colorado (Colorado Zen Center) and in the Maryland suburbs near BWI (Baltimore Zen Center).</description> \n") or die("Error writing channel description");
      
// Loop through query results and print to page.      
      
      while ($row = mysql_fetch_array($res_allblogs2, MYSQL_BOTH)) {            
            fwrite($fpx, "<item> \n") or die("error writing beginning of item");
            fwrite($fpx, "<title>Read the latest Zen Blog: $row[2]</title> \n") or die("error writing item title");
            fwrite($fpx, "<link>http://www.worldzen.org/zenblog.php</link> \n") or die("error writing item link");
            fwrite($fpx, "<description>$row[4]</description> \n") or die("error writing item description");
            fwrite($fpx, "</item> \n") or die("error closing item");
            }
            
            fwrite($fpx, "<item> \n") or die("error writing beginning of item");
            fwrite($fpx, "<title>World Zen News And Photos</title> \n") or die("error writing item title");
            fwrite($fpx, "<link>http://www.worldzen.org/news01.php</link> \n") or die("error writing item link");
            fwrite($fpx, "<description>Recent Events</description> \n") or die("error writing item description");
            fwrite($fpx, "</item> \n") or die("error closing item");
            
            fwrite($fpx, "<item> \n") or die("error writing beginning of item");
            fwrite($fpx, "<title>World Zen Coming Events</title> \n") or die("error writing item title");
            fwrite($fpx, "<link>http://www.worldzen.org/events.php</link> \n") or die("error writing item link");
            fwrite($fpx, "<description>Coming Events</description> \n") or die("error writing item description");
            fwrite($fpx, "</item> \n") or die("error closing item");      

            
      fwrite($fpx, "</channel> \n") or die("Error closing channel");
      fwrite($fpx, "</rss> \n") or die("Error closing rss");
            
// close the XML file that was written to.
fclose($fpx);      
?>
0
 
LVL 8

Accepted Solution

by:
_Marcel_ earned 1500 total points
ID: 19618005
Everything you need for the creation of the XML document you can find here: http://www.tonymarston.net/php-mysql/dom.html

It uses MySQL, but it would be easy to change these statements for the mssql ones.
0
 

Author Comment

by:alateos
ID: 19618043
are both of the above examples using DOM? If so, which is already included in the default installation of php 5
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 16

Expert Comment

by:BongSoo
ID: 19618050
mine is not. its written on the fly. looks like Marcel gave you a much better resource!
0
 

Author Comment

by:alateos
ID: 19618128
well can you guys tell me what the difference is between you the two methods you showed me?
0
 
LVL 16

Expert Comment

by:BongSoo
ID: 19618137
I didn't read his, but mine is basically a very manual process. I instruct PHP to query my database and to manually write my xml file.
0
 

Author Comment

by:alateos
ID: 19618148
Does yours involve the installation of any additional php libraries?
0
 
LVL 16

Expert Comment

by:BongSoo
ID: 19618343
no, it should not.
0
 
LVL 8

Expert Comment

by:_Marcel_
ID: 19618566
The solution I gave should be readily available in PHP5. Using the DOM is the cleanest and safest way to create an XML document. But if the document is really simple, then it is often easier or faster to use the way BongSoo described. But use it only for quick fixes, otherwise go for DOM.
0
 

Author Comment

by:alateos
ID: 19620773
do you have a good example with SQL Server? Cause this is giving me a hard time
0
 
LVL 8

Expert Comment

by:_Marcel_
ID: 19622800
How do you connect to sql server? Have you build connection classes or something? Or do you use mssql_connect() etc. directly? Where do the problems start?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month20 days, 3 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question