472,145 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

XML create with PHP 5 help

I need to create an XML string using PHP5. The examples I have followed seem to
be using out dated libary calls. I tried new_xmldoc() and new DomDocument.
Both get undefined errors. How do I create an XML string where it will look
something like this:

<response>
<data>
<parts>
<part>
<number>bla</number>
<price>9999</price>
<uom>EA</uom>
<locations>
<location>
<id>bla</id>
<qty>9999</qty>
</location>
</locations>
</part>
</parts>
</data>
</response>

Thanks
Gary Quiring

Jul 17 '05 #1
13 6547
You just echo it out to the browser like that, and send the XML content
header.

(Or save it into a variable, for storing into a file)

eg:

<?php
header("Content-type: text/xml");
echo "
<response>\n
<data>\n
<parts>\n
<part>\n
<number>bla</number>\n
</part>\n
</parts>\n
</data>\n
</response>";
?>
"Gary Quiring" <gq******@msn.com> wrote in message
news:5e********************************@4ax.com...
I need to create an XML string using PHP5. The examples I have followed
seem to
be using out dated libary calls. I tried new_xmldoc() and new
DomDocument.
Both get undefined errors. How do I create an XML string where it will
look
something like this:

<response>
<data>
<parts>
<part>
<number>bla</number>
<price>9999</price>
<uom>EA</uom>
<locations>
<location>
<id>bla</id>
<qty>9999</qty>
</location>
</locations>
</part>
</parts>
</data>
</response>

Thanks
Gary Quiring

Jul 17 '05 #2
On Tue, 17 May 2005 14:24:19 +0100, "Alistair Baillie SS2002"
<ab******@cis.strath.ac.uk> wrote:
You just echo it out to the browser like that, and send the XML content
header.

(Or save it into a variable, for storing into a file)

eg:

<?php
header("Content-type: text/xml");
echo "
<response>\n
<data>\n
<parts>\n
<part>\n
<number>bla</number>\n
</part>\n
</parts>\n
</data>\n
</response>";
?>

My output string will vary on the input I need to respond to. That kind of
creation makes it difficult to modify and add childs.
Jul 17 '05 #3
Assumint that the XML output will be approximatly the same, just use for
loops, to add the varying lengths.

IF you mean it will be totally different you could use 3Dimensional arrays,
and then generate the XML string from that.

other than these 2 methods, I'm afraid I cant help you. sorry.

Example for creating RSS output (not using 3d arrays):
<?php
/****
* Blog Off.net
*
* @author: Alistair Baillie
* @website: http://www.alistairbaillie.co.uk
* @version: 1.0
* @sourcepath: \\BO\
*
* The contents of this file and any associated files, including but in no
way
* limited to artwork and images, are copyright (c) 2005 Alistair D Baillie
* Any unauthorised reproduction or distribution is strictly prohibbited!
****/

$_BLOGOFF = true;

require("includes/comon.php");
header("Content-type: text/xml");

// Catch default blog request :)
if ( $_REQUEST['id'] == 0 ) {
echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>Blog Off.net</title>
<link>http://www.blogoff.net/</link>
<description>The 5 latest postings to Blog Off.net Journals</description>
<language>en-gb</language>

<?php
$query = $db->query("SELECT blogs_id, blogs_datetime, blogs_userid,
blogs_title, blogs_entry,
(SELECT Count(*) FROM bo_comments WHERE comments_blogid=row_x.blogs_id) as
numComments,
users_displayname FROM bo_blogs row_x, bo_users WHERE ( blogs_private=0
AND blogs_published=1 AND
users_id=blogs_userid )
ORDER BY blogs_datetime DESC LIMIT 5");

if ( $db->numrows( $query ) > 0 ) {
while ( $theData = $db->fetchrow( $query ) ) {
?>
<item>
<title><?php echo stripslashes( $theData['blogs_title'] ); ?></title>
<link>http://www.blogoff.net/index.php?bpost=<?php echo
$theData['blogs_id']; ?></link>
<description><?php echo stripslashes( $theData['blogs_entry'] );
?></description>
<dc:creator><?php echo stripslashes( $theData['users_displayname'] );
?></dc:creator>
<dc:date><?php echo date("Y-m-d", $theData['blogs_datetime']);
?></dc:date>
</item>
<?php
}
}
?>
</channel>
</rss>
<?php
}

if ( $_REQUEST['id'] > 0 ) {
$id = addslashes($_REQUEST['id']);
echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>Blog Off.net</title>
<link>http://www.blogoff.net/</link>
<description>The 5 latest postings to this persons blog!</description>
<language>en-gb</language>

<?php
$query = $db->query("SELECT blogs_id, blogs_datetime, blogs_userid,
blogs_title, blogs_entry,
(SELECT Count(*) FROM bo_comments WHERE comments_blogid=row_x.blogs_id)
as numComments,
users_displayname FROM bo_blogs row_x, bo_users WHERE ( blogs_private=0
AND blogs_published=1 AND
users_id=blogs_userid AND blogs_userid='$id' )
ORDER BY blogs_datetime DESC LIMIT 5");

if ( $db->numrows( $query ) > 0 ) {
while ( $theData = $db->fetchrow( $query ) ) {
?>
<item>
<title><?php echo stripslashes( $theData['blogs_title'] ); ?></title>
<link>http://www.blogoff.net/index.php?bpost=<?php echo
$theData['blogs_id']; ?></link>
<description><?php echo stripslashes( $theData['blogs_entry'] );
?></description>
<dc:creator><?php echo stripslashes( $theData['users_displayname'] );
?></dc:creator>
<dc:date><?php echo date("Y-m-d", $theData['blogs_datetime']);
?></dc:date>
</item>
<?php
}
}
?>
</channel>
</rss>
<?php
}
?>
Jul 17 '05 #4
Ok, I have done some searching, the new_xmldoc() function was never a valid
PHP function, but it appears to be part of the ZEND Library

See: http://www.zend.com/phpfunc/function.xmldoc.php

"Alistair Baillie SS2002" <ab******@cis.strath.ac.uk> wrote in message
news:42********@nntphost.cis.strath.ac.uk...
Assumint that the XML output will be approximatly the same, just use for
loops, to add the varying lengths.

IF you mean it will be totally different you could use 3Dimensional
arrays, and then generate the XML string from that.

other than these 2 methods, I'm afraid I cant help you. sorry.

Example for creating RSS output (not using 3d arrays):
<?php
/****
* Blog Off.net
*
* @author: Alistair Baillie
* @website: http://www.alistairbaillie.co.uk
* @version: 1.0
* @sourcepath: \\BO\
*
* The contents of this file and any associated files, including but in no
way
* limited to artwork and images, are copyright (c) 2005 Alistair D Baillie
* Any unauthorised reproduction or distribution is strictly prohibbited!
****/

$_BLOGOFF = true;

require("includes/comon.php");
header("Content-type: text/xml");

// Catch default blog request :)
if ( $_REQUEST['id'] == 0 ) {
echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>Blog Off.net</title>
<link>http://www.blogoff.net/</link>
<description>The 5 latest postings to Blog Off.net Journals</description>
<language>en-gb</language>

<?php
$query = $db->query("SELECT blogs_id, blogs_datetime, blogs_userid,
blogs_title, blogs_entry,
(SELECT Count(*) FROM bo_comments WHERE comments_blogid=row_x.blogs_id)
as numComments,
users_displayname FROM bo_blogs row_x, bo_users WHERE ( blogs_private=0
AND blogs_published=1 AND
users_id=blogs_userid )
ORDER BY blogs_datetime DESC LIMIT 5");

if ( $db->numrows( $query ) > 0 ) {
while ( $theData = $db->fetchrow( $query ) ) {
?>
<item>
<title><?php echo stripslashes( $theData['blogs_title'] ); ?></title>
<link>http://www.blogoff.net/index.php?bpost=<?php echo
$theData['blogs_id']; ?></link>
<description><?php echo stripslashes( $theData['blogs_entry'] );
?></description>
<dc:creator><?php echo stripslashes( $theData['users_displayname'] );
?></dc:creator>
<dc:date><?php echo date("Y-m-d", $theData['blogs_datetime']);
?></dc:date>
</item>
<?php
}
}
?>
</channel>
</rss>
<?php
}

if ( $_REQUEST['id'] > 0 ) {
$id = addslashes($_REQUEST['id']);
echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>Blog Off.net</title>
<link>http://www.blogoff.net/</link>
<description>The 5 latest postings to this persons blog!</description>
<language>en-gb</language>

<?php
$query = $db->query("SELECT blogs_id, blogs_datetime, blogs_userid,
blogs_title, blogs_entry,
(SELECT Count(*) FROM bo_comments WHERE comments_blogid=row_x.blogs_id)
as numComments,
users_displayname FROM bo_blogs row_x, bo_users WHERE ( blogs_private=0
AND blogs_published=1 AND
users_id=blogs_userid AND blogs_userid='$id' )
ORDER BY blogs_datetime DESC LIMIT 5");

if ( $db->numrows( $query ) > 0 ) {
while ( $theData = $db->fetchrow( $query ) ) {
?>
<item>
<title><?php echo stripslashes( $theData['blogs_title'] ); ?></title>
<link>http://www.blogoff.net/index.php?bpost=<?php echo
$theData['blogs_id']; ?></link>
<description><?php echo stripslashes( $theData['blogs_entry'] );
?></description>
<dc:creator><?php echo stripslashes( $theData['users_displayname'] );
?></dc:creator>
<dc:date><?php echo date("Y-m-d", $theData['blogs_datetime']);
?></dc:date>
</item>
<?php
}
}
?>
</channel>
</rss>
<?php
}
?>

Jul 17 '05 #5
Gary Quiring wrote:
I need to create an XML string using PHP5. The examples I have
followed seem to be using out dated libary calls. I tried
new_xmldoc() and new DomDocument. Both get undefined errors. How do
I create an XML string where it will look something like this:


<snip>

http://www.zend.com/php5/articles/php5-xmlphp.php

However, the undefined errors seem that for some reason you don't have the
DOM extension in your PHP installation. What version of PHP5 do you use?

Berislav
Jul 17 '05 #6
On Wed, 18 May 2005 09:08:38 +0200, "Berislav Lopac"
<be************@lopsica.com> wrote:
Gary Quiring wrote:
I need to create an XML string using PHP5. The examples I have
followed seem to be using out dated libary calls. I tried
new_xmldoc() and new DomDocument. Both get undefined errors. How do
I create an XML string where it will look something like this:


<snip>

http://www.zend.com/php5/articles/php5-xmlphp.php

However, the undefined errors seem that for some reason you don't have the
DOM extension in your PHP installation. What version of PHP5 do you use?

Berislav

PHP 5.04

I did configure with dom support. Did I do it wrong?

Here is the php info:

System Linux ae1 2.6.9-1.667smp #1 SMP Tue Nov 2 14:59:52 EST 2004 i686
Build Date May 13 2005 15:24:06
Configure Command './configure' '--with-informix=/opt/informix'
'--with-apxs2=/usr/sbin/apxs' '--with-config-file-path=/etc'
'--enable-force-cgi-redirect' '--enable-pic' '--enable-inline-optimization'
'--with-bz2' '--with-curl' '--enable-soap' '--with-dom' '--with-gettext'
'--with-ncurses' '--with-gmp' '--with-iconv' '--with-layout=GNU'
'--enable-bcmath' '--enable-exif' '--enable-ftp' '--enable-magic-quotes'
'--enable-safe-mode' '--enable-sockets' '--enable-sysvsem' '--enable-sysvshm'
'--enable-discard-path' '--enable-track-vars' '--enable-trans-sid' '--enable-yp'
'--enable-wddx' '--without-oci8' '--with-imap=shared' '--with-imap-ssl'
'--with-kerberos' '--with-ldap=shared' '--enable-memory-limit' '--enable-bcmath'
'--enable-shmop' '--enable-versioning' '--enable-calendar' '--enable-dbx'
'--enable-dio' '--enable-mcal'


Jul 17 '05 #7
Try the following Dom syntax:
And try to put some of your own stuff in it.
/* Dom document */
$dom = new DOMDocument("1.0","iso-8859-1");
/* Create all elements */
$response = $dom->createElement("response"); //root element
$data = $dom->createElement("data");
$parts = $dom->createElement("parts");
$part = $dom->createElement("parts");
$number = $dom->createElement("number","bla");
$price = $dom->createElement("price", "9999");

/* Now place them in the correct place in the tree */

$response->appendChild($data);
$data->appendChild($parts);
$parts->appendChild($part);
$part->appendChild($number);
$part->appendChild($price);

$dom->appendChild($response);

echo $dom->saveXML();
Jul 17 '05 #8
On Wed, 18 May 2005 15:14:36 +0200, Azeus <IF*************************@MP.com>
wrote:
Try the following Dom syntax:
And try to put some of your own stuff in it.
/* Dom document */
$dom = new DOMDocument("1.0","iso-8859-1");
/* Create all elements */
$response = $dom->createElement("response"); //root element
$data = $dom->createElement("data");
$parts = $dom->createElement("parts");
$part = $dom->createElement("parts");
$number = $dom->createElement("number","bla");
$price = $dom->createElement("price", "9999");

/* Now place them in the correct place in the tree */

$response->appendChild($data);
$data->appendChild($parts);
$parts->appendChild($part);
$part->appendChild($number);
$part->appendChild($price);

$dom->appendChild($response);

echo $dom->saveXML();

What output should I see on the browser? I only see bla9999. It does not show
the XML file with the tags?

Thanks
Gary

Jul 17 '05 #9
Gary Quiring wrote:
On Wed, 18 May 2005 15:14:36 +0200, Azeus <IF*************************@MP.com> wrote:
Try the following Dom syntax:
And try to put some of your own stuff in it.

<... SNIP CODE ...> What output should I see on the browser? I only see bla9999. It

does not show the XML file with the tags?


Now view the source in the browser. There's your XML. If you want the
browser window to display the XML, you need to send the right
Content-Type MIME header (which is text/xml).

--
Oli

Jul 17 '05 #10
On 18 May 2005 12:30:58 -0700, "Oli Filth" <ca***@olifilth.co.uk> wrote:
Gary Quiring wrote:
On Wed, 18 May 2005 15:14:36 +0200, Azeus

<IF*************************@MP.com>
wrote:
>Try the following Dom syntax:
>And try to put some of your own stuff in it.
>
><... SNIP CODE ...> >

What output should I see on the browser? I only see bla9999. It

does not show
the XML file with the tags?


Now view the source in the browser. There's your XML. If you want the
browser window to display the XML, you need to send the right
Content-Type MIME header (which is text/xml).

If I view the source I still see bla9999
Jul 17 '05 #11
On 18 May 2005 12:30:58 -0700, "Oli Filth" <ca***@olifilth.co.uk> wrote:
Gary Quiring wrote:
On Wed, 18 May 2005 15:14:36 +0200, Azeus

<IF*************************@MP.com>
wrote:
>Try the following Dom syntax:
>And try to put some of your own stuff in it.
>
><... SNIP CODE ...> >

What output should I see on the browser? I only see bla9999. It

does not show
the XML file with the tags?


Now view the source in the browser. There's your XML. If you want the
browser window to display the XML, you need to send the right
Content-Type MIME header (which is text/xml).

Ok I wrote the object to a file and I see the tags. I don't know why my browser
is not showing them when viewing the source.

Thanks
Gary
Jul 17 '05 #12
did you send the txt/xml header?

Jul 17 '05 #13
Gary Quiring <gq******@msn.com> wrote:

My output string will vary on the input I need to respond to. That kind of
creation makes it difficult to modify and add childs.


You have to judge it for yourself, of course, but it is often just as easy
and is ALWAYS more efficient to create XML without the help of an XML
library.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 17 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by JVince | last post: by
4 posts views Thread by Richard Tierney | last post: by
1 post views Thread by Diego F. | last post: by
reply views Thread by PK | last post: by
2 posts views Thread by Mike TI | last post: by
1 post views Thread by jack | last post: by
4 posts views Thread by Coleen | last post: by
7 posts views Thread by Supriya | last post: by
pentahari
5 posts views Thread by pentahari | last post: by
reply views Thread by Saiars | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.