SimpleXMLElement - how to format output of XML result

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rubendoliveira
Forum Newbie
Posts: 4
Joined: Tue Dec 28, 2010 8:04 am

SimpleXMLElement - how to format output of XML result

Post by rubendoliveira »

Hi everyone,

I have data in a database and I'm getting that data and transforming it in XML with PHP. Everything works so far, however, I need to format the output result.

At this point, the elements from the database get all together in the same line, I need to separate each row in a new line.. making it pretty and easy to read.

This is my code:

Code: Select all

<?php
	$sxe = new SimpleXMLElement('<bookstore/>');
				
	include ("connection.php");
				
	$myquery = 'SELECT * FROM bookstoretb';
	$sql = mysql_query($myquery);
		if (!$sql)
		{
			error_log("Query $myquery failed: " . mysql_error());
		}
				
	while ( $row = mysql_fetch_array($sql, MYSQL_ASSOC) ) {
		$line = $sxe->addChild('book');
					 
		foreach ( $row as $fieldName => $value)
			$line->addChild($fieldName, $value. " \n ");
						
	}
	echo $sxe->asXML();
?>
Thank you in advance and wishes of a good year!
rcrd.ortiz
Forum Newbie
Posts: 11
Joined: Sun Dec 26, 2010 10:46 am

Re: SimpleXMLElement - how to format output of XML result

Post by rcrd.ortiz »

Hi,

It really depends on what level of neatness you like and/or want. I'm not very neat so I would just multi line, I don't need indentation or other fancy stuff. I would just add new lines after closing tags like this:

Code: Select all

<?php
        $sxe = new SimpleXMLElement('<bookstore/>');
                                
        include ("connection.php");
                                
        $myquery = 'SELECT * FROM bookstoretb';
        $sql = mysql_query($myquery);
                if (!$sql)
                {
                        error_log("Query $myquery failed: " . mysql_error());
                }
                                
        while ( $row = mysql_fetch_array($sql, MYSQL_ASSOC) ) {
                $line = $sxe->addChild('book');
                                         
                foreach ( $row as $fieldName => $value)
                        $line->addChild($fieldName, $value. " \n ");
                                                
        }
        echo str_replace( '/>', "/>\n", $sxe->asXML());
?>
If this is too simple for you make a parser.

Cheers
rubendoliveira
Forum Newbie
Posts: 4
Joined: Tue Dec 28, 2010 8:04 am

Re: SimpleXMLElement - how to format output of XML result

Post by rubendoliveira »

Hi,

Thank you for your answer.

However, the result is almost the same. I have two books in my database and the information of both are presented on the same line, even with the new line you added. I want it simple as well, I just want each book on a new line.

Any idea of what's wrong?


Thanks once again
rcrd.ortiz
Forum Newbie
Posts: 11
Joined: Sun Dec 26, 2010 10:46 am

Re: SimpleXMLElement - how to format output of XML result

Post by rcrd.ortiz »

Ah I see, at work we use a different structure, we add stuff as attributes instead of as elements, anyways here is what you want:

Code: Select all

<?php
/**
 * Supply a SimpleXMLElement to get back the formatted xml as string.
 * 
 * @param SimpleXMLElement $s
 * @return string
 */
function formatXML( SimpleXMLElement $s )
{
    $f = dom_import_simplexml( $s )->ownerDocument;
    $f->formatOutput = TRUE;
    return (string)$f->saveXML();
}


$a = new SimpleXMLElement( '<data/>' );
$a->addChild( 'book' );
$a->book->addChild( 'title', 'Really good book' );

echo formatXML( $a );

?>
Cheers,

Rcrd
Post Reply