Jump to content

Generating Xml With Php?


son

Recommended Posts

After spending hours exporting XML from MySQL database and then adjusting to XSD schema I wondered if it was possible to use somehow PHP in XML file to dynamically create the XML file? Or how would you go about this issue?Son

Link to comment
Share on other sites

Erm... why? What do you want to do with the XML file? Just make a change and output it?If the file is small enough, you can use DOM or SimpleXML to open it, find whatever you need, and edit/replace it with whatever you need and then output it. The exact functions depend on your method of choise. For example, in DOM it would be something like:

$dom = new DOMDocument;$dom->loadXML($xml); //Assuming $xml holds the XML as a string. Use load() if it's in a file//From here on, approaches to select your data vary. I'll use XPath$xpath = new DOMXPath($dom);$node = $xpath->query('/*/*[2]/*[1]')->item(0); //I selected the first element from the second child of the root element$node->nodeValue = 'This was changed by DOM. Ha!'; //The contents of this element (including its descendants) is going to be replaced by this text.echo $dom->saveXML(); //We're going to directly display the file on screen. If you want to instead save it as a file, use save()

If there's a potential that the file will grow in very large (15MBs or more) in size, there are alternative approaches, but they're harder to implement and demonstrate, so first either describe the complete scenario, or see if this works.

Link to comment
Share on other sites

That still doesn't describe what you have already.Have you gotten MySQL to generate/export XML files ('cause it sure sounds like that from your description)? Do you have an XSD that the finished file must validate against (say, is it required by that external website you're talking about)? Is MySQL creating such files dynamically itself, or must you cast a certain query? If so, what's the query, and does it produce the exact XML you need?What I just gave you above assumes you have a file that MySQL has genrated in one way or another (perhaps in an earlier stage of the PHP execution) which is not exactly what you want, and lets you modify it, and finally output it, thus giving it to whoever open that PHP file (including the external website).Try to describe the complete workflow you want (the site admin does this, PHP must dynamically do this, the end user must see this, the external website must see that)

Link to comment
Share on other sites

That still doesn't describe what you have already.Have you gotten MySQL to generate/export XML files ('cause it sure sounds like that from your description)? Do you have an XSD that the finished file must validate against (say, is it required by that external website you're talking about)? Is MySQL creating such files dynamically itself, or must you cast a certain query? If so, what's the query, and does it produce the exact XML you need?What I just gave you above assumes you have a file that MySQL has genrated in one way or another (perhaps in an earlier stage of the PHP execution) which is not exactly what you want, and lets you modify it, and finally output it, thus giving it to whoever open that PHP file (including the external website).Try to describe the complete workflow you want (the site admin does this, PHP must dynamically do this, the end user must see this, the external website must see that)
The current situation is: All our product data is in MySQL database and in phpMyAdmin I run a query as:SELECT `range_id`, `vat_price` AS price, `price_before_discount`, `description`, `online_name` AS name, `id`, `width`, `depth`, `height`, `colour`, `size`, `supplier`, `type`, `categories`, `styles`, `manufacturer`, `availability`, `delivery_within`, `finish`, `materials` FROM `products` WHERE range_id !=3with the resulting rows being exported into XML, which I then manually modfiy to conform with existing XSD (you are right, this one is given by external website). The changes I need to make:- in db field the styles are as comma-separated list, but XSD requires to have the styles as <styles><style>first style</style><style>second style</style></styles> (this applies to four other fields)- add <url/> tag for each product, which is same web page, but url has two varying parameters for range_id and id- add 3 <image_url/> tags with <image_urls/> parent. Examples are 1-34.jpg, 2-34.jpg and 3-34.jpg for product id 34 and 1-13.jpg, 2-13.jpg and 3-13.jpg for product id 13My hope is: To query in a way, so all changes I need to make are automatically done my my query with all data dynamically coming from database. So, if there are changes in product data they would automatically be reflected in XML. The display of our products on our website (which is generated with PHP from MYSQL database) is independent from the XML file. Not all our products are meant for XML feed (currently one range is taken out).I hope I was a bit clearer this time:-)Son
Link to comment
Share on other sites

Much clearer.Your best bet is to programmatically generate the XML manually within PHP by plain string concatenation (or DOM, but that may seem harder). Ideally, you may (and should) validate your final results at the end. Without being completely aware of the end code you need, I'd go for something like:

$mysqli = new mysqli('localhost', 'root', '', 'db'); /* Change the DB details as needed. */$queryResult = $mysqli->query('SELECT `range_id`, `vat_price` AS price, `price_before_discount`, `description`, `online_name` AS name, `id`, `width`, `depth`, `height`, `colour`, `size`, `supplier`, `type`, `categories`, `styles`, `manufacturer`, `availability`, `delivery_within`, `finish`, `materials` FROM `products` WHERE range_id !=3'); /* Feel free to make the query dynamic, as long as it returns the same fields */$xml = '<?xml version="1.0" encoding="UTF-8"?><root>'; /*Chnage the name of the root element here if you must */while($row = $queryResult->fetch_assoc()) {	$xml .= '<item><url>/productPage.php?range_id=' . $row['range_id'] . '&id=' . $row['id'] . '</url></item>';//Adjust as needed to generate whatever must happen for each row}$xml .= '</root>'; /* If you were to rename the root element, don't forget to change it here as well. *///$xml should now contain the XML you want.//Now we just verify it, and output it on success. $dom = new DOMDocument;if ($dom->loadXML($xml)) {	if ($dom->schemaValidate('schema.xsd')) {		header('Content-Type: application/xml');		echo $dom->saveXML(); /* You could have just $xml, but let's keep it on the safe side */	}else {		echo 'The generated XML is invalid (but well-formed). The exact error message is:' . libxml_get_last_error()->message . ' on line ' . libxml_get_last_error()->line;	}}else {	echo 'The generated XML is not well-formed. The exact error message is:' . libxml_get_last_error()->message . ' on line ' . libxml_get_last_error()->line;}

Link to comment
Share on other sites

Just before I go ahead to test it myself (thanks for great example): What would the URL of the generated .xml file be? Kind of did not get this one|-)Son
The output of the PHP file is the XML. No need to keep it in anoter URL. Every time the external site wants the XML, it goes to this PHP file's URL, the DB is queried, and the XML text is generated. The external website should not worry about the file having a ".php" extension. It may only care about its MIME type, and if it does - that's what the header() call is for.If you want, you could instead replace
header('Content-Type: application/xml');$dom->saveXML();

with

$dom->save('file.xml');

but in this case, you'll have to place all of the code within your update script (after the update part), so that 'file.xml' is updated along with the DB. Also, if the DB can be updated by multiple means (like, with PHPMyAdmin AND your update script), changes done with a tool other than your update script will not be reflected in the XML file. When you generate the XML on the fly - as with my former example - changes are reflected immediatly, no matter the way the information is updated.

Link to comment
Share on other sites

The output of the PHP file is the XML. No need to keep it in anoter URL. Every time the external site wants the XML, it goes to this PHP file's URL, the DB is queried, and the XML text is generated. The external website should not worry about the file having a ".php" extension. It may only care about its MIME type, and if it does - that's what the header() call is for.If you want, you could instead replace
header('Content-Type: application/xml');$dom->saveXML();

with

$dom->save('file.xml');

but in this case, you'll have to place all of the code within your update script (after the update part), so that 'file.xml' is updated along with the DB. Also, if the DB can be updated by multiple means (like, with PHPMyAdmin AND your update script), changes done with a tool other than your update script will not be reflected in the XML file. When you generate the XML on the fly - as with my former example - changes are reflected immediatly, no matter the way the information is updated.

Thanks for your input. Will work with your example and see if I get it working. Really appreciate your help:-)Son
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...