SimpleXMLElement - how to format output of XML result

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:


<?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. " 
 ");
						
	}
	echo $sxe->asXML();
?>

Thank you in advance and wishes of a good year!