/    Sign up×
Community /Pin to ProfileBookmark

Creating a XML document from MYSQL

This is what i need to do:
1: Create an empty XML document
2: connect to the database
3: retrieve all blog entries from database
4: create an entry node for each record set, and add the entry node as a child of entries
5: return the XML document as a web page

Below is what i have so far.

[code]

<?php
//filename
$filename = “../xml/blog-dbxml.xml”;

//create empty XML document
$rawBlog = “<?xml version=”1.0″ encoding=”utf-8″ ?>”;
$rawBlog .= “<blog><title>YouCube – The Blog for Cube Puzzlers</title>”;
$rawBlog .= “<author>Puzzler Ruby</author><entries></entries></blog>”;

//Compile xml document header texts
$xml = new SimpleXmlElement($rawBlog);

//database varibale
$dbhost = ‘xxx’;
$username = ‘xxx’;
$password = ‘xxx’;
$dbname = ‘xxxx’;
$port = ‘xxxx’;

//connect to mysql and database
$dbc = mysqli_connect($dbhost, $username, $password, $dbname, $port)
or die(‘Error Connecting to Database: ‘ . mysql_error());

//select database into variable
$query = “select * from Blog;”;
$result = mysqli_query($dbc, $query)
or die(“Could not complete database query at line 26”. mysql_error());

//max number of records
$num = mysqli_num_rows($result);

[/code]

I am having trouble with the actual array part now, how do i get the values of the array and send them to a xml document?

Here is code form another ‘add to xml document’ i had.

[code]
/* Add the new blog entry as a child node
$entry = $xml->entries->addChild(“entry”);
$entry->addChild(“date”, $_REQUEST[“date”]);
$entry->addChild(“body”, stripslashes($_REQUEST[“body”]));
if ($_REQUEST[“image”] != “”)
$entry->addChild(“image”, $_REQUEST[“image”]);

// Write the entire blog to the file
$file = fopen($filename, ‘w’);
fwrite($file, $xml->asXML());
fclose($file);
?>
*/

[/code]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@JaxyralauthorJun 08.2010 — Alright so I have a php file that works now it creates a XML file with the entries from database. The code is Below with explanations of what should be happening.

Here is an example of what the output should look like. From an earlier project that just took a xml document and displayed it. The output should be the same where the blog entries load: http://ix.cs.uoregon.edu/~hoyle/281/fp/youcube13.html

[B]youcube-db.html[/B]

The loadBlog() functions should send an ajax request for getBlogXML.php, which returns the blog entries as an xml document.

<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;YouCube - The Blog for Cube Puzzlers&lt;/title&gt;

<i> </i>&lt;script type="text/javascript" src="../js/ajax.js"&gt; &lt;/script&gt;
<i> </i>&lt;script type="text/javascript" src="../js/date.js"&gt; &lt;/script&gt;

<i> </i>&lt;script type="text/javascript"&gt;

<i> </i> // Custom Date function to display a date in MM/DD/YYYY format
<i> </i> Date.prototype.shortFormat = function() {
<i> </i> return (this.getMonth() + 1) + "/" + this.getDate() + "/" + this.getFullYear();
<i> </i> }

<i> </i> // Blog object constructor
<i> </i> function Blog(body, date, image) {
<i> </i> // Assign the properties
<i> </i> this.body = body;
<i> </i> this.date = date;
<i> </i> this.image = image;
<i> </i> }

<i> </i> // Return a string representation of the blog entry
<i> </i> Blog.prototype.toString = function() {
<i> </i> return "[" + this.date.shortFormat() + "] " + this.body;
<i> </i> };

<i> </i> // Return a formatted HTML representation of the blog entry
<i> </i> Blog.prototype.toHTML = function(highlight) {
<i> </i> // Use a gray background as a highlight, if specified
<i> </i> var blogHTML = "";
<i> </i> blogHTML += highlight ? "&lt;p style='background-color:#EEEEEE'&gt;" : "&lt;p&gt;";

<i> </i> // Generate the formatted blog HTML code
<i> </i> if (this.image) {
<i> </i> blogHTML += "&lt;strong&gt;" + this.date.shortFormat() + "&lt;/strong&gt;&lt;br /&gt;&lt;table&gt;&lt;tr&gt;&lt;td&gt;&lt;img src='" +
<i> </i> this.image + "'/&gt;&lt;/td&gt;&lt;td style='vertical-align:top'&gt;" + this.body + "&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;em&gt;" +
<i> </i> this.signature + "&lt;/em&gt;&lt;/p&gt;";
<i> </i> }
<i> </i> else {
<i> </i> blogHTML += "&lt;strong&gt;" + this.date.shortFormat() + "&lt;/strong&gt;&lt;br /&gt;" + this.body +
<i> </i> "&lt;br /&gt;&lt;em&gt;" + this.signature + "&lt;/em&gt;&lt;/p&gt;";
<i> </i> }
<i> </i> return blogHTML;
<i> </i> };

<i> </i> // See if the blog body contains a string of text
<i> </i> Blog.prototype.containsText = function(text) {
<i> </i> return (this.body.toLowerCase().indexOf(text.toLowerCase()) != -1);
<i> </i> };

<i> </i> // Set the blog-wide signature
<i> </i> Blog.prototype.signature = "by Puzzler Ruby";

<i> </i> // Sort helper to sort blog entries in reverse chronologi‚à Çcal order (most recent first)
<i> </i> Blog.blogSorter = function(blog1, blog2) {
<i> </i> return blog2.date - blog1.date;
<i> </i> };

<i> </i> // Global array of blog entries
<i> </i> var blog = new Array();

<i> </i> // Global Ajax request
<i> </i> var ajaxReq = new AjaxRequest();

<i> </i> // Load the blog from an XML doc on the server using Ajax
<i> </i> function loadBlog() {
<i> </i> document.getElementById("blog").innerHTML = "&lt;img src='../images/wait.gif' alt='Loading...' /&gt;";
<i> </i> ajaxReq.send("GET", "../xml/blog-dbxml.xml", handleRequest);
<i> </i> }

<i> </i> // Handle the Ajax request
<i> </i> function handleRequest() {
<i> </i> if (ajaxReq.getReadyState() == 4 &amp;&amp; ajaxReq.getStatus() == 200) {
<i> </i> // Store the XML response data
<i> </i> var xmlData = ajaxReq.getResponseXML().getElementsByTagName("blog")[0];

<i> </i> // Set the blog-wide signature
<i> </i> Blog.prototype.signature = "by " + getText(xmlData.getElementsByTagName("author")[0]);

<i> </i> // Create the array of Blog entry objects
<i> </i> var entries = xmlData.getElementsByTagName("entry");
<i> </i> for (var i = 0; i &lt; entries.length; i++) {
<i> </i> // Create the blog entry
<i> </i> blog.push(new Blog(getText(entries[i].getElementsByTagName("body")[0]),
<i> </i> new Date(getText(entries[i].getElementsByTagName("date")[0])),
<i> </i> getText(entries[i].getElementsByTagName("image")[0])));
<i> </i> }

<i> </i> // Enable the blog buttons
<i> </i> document.getElementById("search").disabled = false;
<i> </i> document.getElementById("showall").disabled = false;
<i> </i> document.getElementById("viewrandom").disabled = false;

<i> </i> // Show the blog
<i> </i> showBlog(5);
<i> </i> }
<i> </i> }

<i> </i> // Show the list of blog entries
<i> </i> function showBlog(numEntries) {
<i> </i> // First sort the blog
<i> </i> blog.sort(Blog.blogSorter);

<i> </i> // Adjust the number of entries to show the full blog, if necessary
<i> </i> if (!numEntries)
<i> </i> numEntries = blog.length;

<i> </i> // Show the blog entries
<i> </i> var i = 0, blogListHTML = "";
<i> </i> while (i &lt; blog.length &amp;&amp; i &lt; numEntries) {
<i> </i> blogListHTML += blog[i].toHTML(i % 2 == 0);
<i> </i> i++;
<i> </i> }

<i> </i> // Set the blog HTML code on the page
<i> </i> document.getElementById("blog").innerHTML = decodeURIComponent(blogListHTML);
<i> </i> }

<i> </i> // Search the list of blog entries for a piece of text
<i> </i> function searchBlog() {
<i> </i> var searchText = document.getElementById("searchtext").value;
<i> </i> for (var i = 0; i &lt; blog.length; i++) {
<i> </i> // See if the blog entry contains the search text
<i> </i> if (blog[i].containsText(searchText)) {
<i> </i> alert(blog[i]);
<i> </i> break;
<i> </i> }
<i> </i> }

<i> </i> // If the search text wasn't found, display a message
<i> </i> if (i == blog.length)
<i> </i> alert("Sorry, there are no blog entries containing the search text.");
<i> </i> }

<i> </i> // Display a randomly chosen blog entry
<i> </i> function randomBlog() {
<i> </i> // Pick a random number between 0 and blog.length - 1
<i> </i> var i = Math.floor(Math.random() * blog.length);
<i> </i> alert(blog[i]);
<i> </i> }

<i> </i> // Get the text content of an HTML element
<i> </i> function getText(elem) {
<i> </i> var text = "";
<i> </i> if (elem) {
<i> </i> if (elem.childNodes) {
<i> </i> for (var i = 0; i &lt; elem.childNodes.length; i++) {
<i> </i> var child = elem.childNodes[i];
<i> </i> if (child.nodeValue)
<i> </i> text += child.nodeValue;
<i> </i> else {
<i> </i> if (child.childNodes[0])
<i> </i> if (child.childNodes[0].nodeValue)
<i> </i> text += child.childNodes[0].nodeValue;
<i> </i> }
<i> </i> }
<i> </i> }
<i> </i> }
<i> </i> return text;
<i> </i> }
<i> </i>&lt;/script&gt;
&lt;/head&gt;

&lt;body onload="loadBlog(); updateBlog();"&gt;
&lt;h3&gt;YouCube - The Blog for Cube Puzzlers&lt;/h3&gt;
&lt;img src="../images/cube.png" alt="YouCube" /&gt;
&lt;input type="button" id="search" value="Search the Blog" disabled="disabled" onclick="searchBlog();" /&gt;
&lt;input type="text" id="searchtext" name="searchtext" value="" /&gt;
&lt;div id="blog"&gt;&lt;/div&gt;
&lt;input type="button" id="showall" value="Show All Blog Entries" disabled="disabled" onclick="showBlog();" /&gt;
&lt;input type="button" id="viewrandom" value="View a Random Blog Entry" disabled="disabled" onclick="randomBlog();" /&gt;
&lt;/body&gt;
&lt;/html&gt;


[B]getBlogXML.php[/B]

Builds and returns an xml document from the entries stored in MSQL blog table.

<i>
</i>&lt;?php
//filename
$filename = "../xml/blog-dbxml.xml";

//create empty XML document Headers
$rawBlog = "&lt;?xml version="1.0" encoding="utf-8" ?&gt;";
$rawBlog .= "&lt;blog&gt;&lt;title&gt;YouCube - The Blog for Cube Puzzlers&lt;/title&gt;";
$rawBlog .= "&lt;author&gt;Puzzler Hoyle&lt;/author&gt;";
$rawBlog .= "&lt;link&gt;http://ix.cs.uoregon.edu/~hoyle/281/xml/blog-dbxml.xml&lt;/link&gt;";
$rawBlog .= "&lt;info&gt;THIS PROGRAM OVERWRITES 281/XML/BLOG-DBXML.XML&lt;/info&gt;&lt;entries&gt;&lt;/entries&gt;&lt;/blog&gt;";

//Compile xml document header texts
$xml = new SimpleXmlElement($rawBlog);

// Create File and open
$file = fopen($filename, 'w');

//database connect varibales
$dbhost = 'xxxxxx';
$username = 'xxxxx';
$password = 'xxxxx';
$dbname = 'xxxxx';
$port = 'xxxxx';

//connect to mysql and database
$dbc = mysqli_connect($dbhost, $username, $password, $dbname, $port)
or die('Error Connecting to Database: ' . mysql_error());

//select database into variable
$query = "select * from Blog;";
$result = mysqli_query($dbc, $query)
or die("Result One Database Query". mysql_error());

while($row = mysqli_fetch_array($result)){
$entry = $xml-&gt;entries-&gt;addChild("entry");
$entry-&gt;addChild("date", $row[0]);
$entry-&gt;addChild("body", $row[1]);
$entry-&gt;addChild("image", $row[2]);
}

fwrite($file, $xml-&gt;asXML());
fclose($file);

?&gt;
Copy linkTweet thisAlerts:
@katierosyJun 09.2010 — Please read little more on dom xml, while writing your code, just keep it close to you because it makes it difficult with little bit of error.
×

Success!

Help @Jaxyral spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...