> -----Original Message-----
> you could easily just print the xml manually.
>
> <dvd>
> <titleID>foo</titleID>
> <bar>etc</bar>
> </dvd>
>
> or, i believe mysql will return XML output but i'm not sure a PHP
> function exists to do that.
>
> no need to use fancy DOM functions, outputting XML is easy, parsing it
> is when you need to use the special functions and such.
Here's the 'functions_xml.inc.php' file I use, it has served me well and
handles I believe all tag cases, personally I'm a big fan of using the
attributes as they're most easily parsed out in the PHP DOM functions:
--------------------------------8<--------------------------------------
---
<?php
/**
* Aborts XML output with an Error message
*
* @return string xml formatted tags and data
* @param string $message the error message to output
* @version 1.3
* @date 07/14/05
*/
function XML_exit($message)
{
header( "Content-type: text/xml" );
print "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
//TODO: [dv] we should probably put an error code so that it's
easier for the scripts to parse
//print xmltag('error', array('code'=>'TBD',
'string'=>$message));
print xmltag('error',null, 1);
print xmltag('code','TBD');
print xmltag('string',$message);
print xmltag('error',null, 2);
exit;
}
/**
* Returns a well formed lowercase XML tag string suitable for printing
to screen
*
* tagType=1: is like tagType=3, but without the />
* tagType=2: is the closing </xmltag> only.
*
* tagType=3:
* <xmltag foo=bar />
*
* also note that array('foo'=>'bar','__VALUE'='1234') will
generate:
* <xmltag foo=bar>1234</xmltag>
*
* tagType=4:
* <xmltag>
* <foo>bar</foo>
* </xmltag>
*
* @return string xml formatted tags and data
* @param string $xmltag is the tag name to surround
* @param mixed $data data to output between the tags, if
$data is an array, it will be expanded as KEY=VALUE in the tag. If there
is an array element __VALUE that will be a value between XML tags after
the list of attributes
* @param int $tagType 1=start tag only, 2=end tag only,
3=default, 4=verbose tag
* @todo use recursion to get nested arrays working, especially
for tagType=4
* @version 1.6
* @date 01/16/06
*/
function xmltag($xmltag, $data, $tagType = 3)
{
// convert spaces to underscores and makes it lowercase as per
XML specification
$xmltag = strtolower(str_replace(' ','_', $xmltag ));
// encode all characters that need to be converted for export
into XML
$data = xmlentities($data);
//we put this first, as it is used most frequently
if ($tagType == 3)
{
if (isset($data))
{
if (is_array($data))
{
if (isset($data['__VALUE']))
{
$value_data = $data['__VALUE'];
unset($data['__VALUE']);
}
$tmp = '<'.$xmltag;
foreach($data as $key => $value)
{
if ( is_bool($value) ) $value =
($value === true)?'true':'false';
$tmp .= '
'.$key.'="'.$value.'"';
}
if (isset($value_data))
{
if ( is_bool($value_data) )
$value_data = ($value_data === true)?'true':'false';
$tmp .=
">".$value_data."</".$xmltag.">\r\n";
}
else
$tmp .= " />\r\n";
return $tmp;
}
else
{
if ( is_bool($data) ) $data = ($data ===
true)?'true':'false';
return
'<'.$xmltag.'>'.$data.'</'.$xmltag.">\r\n";
}
}
else return '<'.$xmltag." />\r\n";
} //tag type 3
if ($tagType == 1)
{
$tmp = '<'.$xmltag;
if (is_array($data))
{
foreach($data as $key => $value)
{
if ( is_bool($value) ) $value = ($value
=== true)?'true':'false';
$tmp .= ' '.$key.'="'.$value.'"';
}
}
$tmp .= ">\r\n";
return $tmp;
} //tag type 1
if ($tagType == 2) return '</'.$xmltag.">\r\n";
if ($tagType == 4)
{
if (isset($data))
{
if (is_array($data))
{
$tmp = '<'.$xmltag.">\r\n";
foreach($data as $key => $value)
{
if ( is_bool($value) ) $value =
($value === true)?'true':'false';
$tmp .=
'<'.$key.'>'.$value.'<'.$key."/>\r\n";
}
$tmp = '</'.$xmltag.">\r\n";
return $tmp;
}
else
{
if ( is_bool($data) ) $data = ($data ===
true)?'true':'false';
return
'<'.$xmltag.'>'.$data.'</'.$xmltag.">\r\n";
}
}
else return '<'.$xmltag." />\r\n";
} //tag type 4
}
/**
* Print out an array in XML form (useful for debugging)
* @access public
* @param string $myArray the array to output in XML format
* @author Daevid Vincent [EMAIL PROTECTED]
* @version 1.0
* @date 07/19/05
* @todo It would be nice if we could extract the array's variable name
and output that as an attribute
*/
function print_r_xml($myArray)
{
print xmltag('ARRAY', null, 1);
foreach($myArray as $k => $v)
{
if (is_array($v))
print_r_xml($v);
else
print xmltag($k,htmlspecialchars($v));
}
print xmltag('ARRAY', null, 2);
}
?>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php