5 Replies - 8219 Views - Last Post: 14 November 2010 - 09:52 AM

#1 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10390
  • View blog
  • Posts: 38,447
  • Joined: 27-December 08

Week #37- XML

Post icon  Posted 08 November 2010 - 10:23 AM

This week's challenge is XML.

XML is a markup language for encoding data in machine (and human) readable format. It is designed for simplicity and portability. XML tags are written using angle brackets <>, with the tag name and attributes inside in the format <tag attribute="value">.

Some Ideas for Challenges:
-Define your own XML format. This can be done using a number of schemas, including a Document Type Definition (DTD) or an XML Schema.
-Experiment with some XML parsing APIs including SAX, DOM, and a pull parser. Possibly work with the DIC API.
-Layout XML using CSS or XSLT
-Write a program to generate XML from existing data, perhaps including a SQL result set

Resources:
-W3Schools
-W3C XML Documentation
-KYA's Blog Entries on Parsing XML in Java and C#
-DIC API Feedback and Support forum

My Entry: My Write Columns of Table To XML snippet.
/*
   @param $result: the result set from the SQL query
   @param $cols: the array of column names
   @param $filename: the name of the File to create
   @param $tagName: The name of the container tag for each row
   <pre>The File is assumed to not exist</pre>
*/
public function queryToXML($result, array $cols, $filename, $tagName){
    $file = fopen($filename, "w"); //open up file stream

    //write initial XML heading
    fwrite($file, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
    
    //foreach row
    while($row = mysql_fetch_array($result)){
        fwrite($file, "<".$tagName.">\n");
        
        //write data using col name as tag
        foreach($cols as $tag){
             fwrite($file,"\t<".$tag.">".$row[$tag]."</".$tag.">\n");
        }//end foreach
         
        //close row tag
        fwrite($file,"</".$tagName.">\n"); 
    }//end while

    fclose($file);
}//end queryToXML()



Is This A Good Question/Topic? 0
  • +

Replies To: Week #37- XML

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: Week #37- XML

Posted 08 November 2010 - 10:50 AM

Since the vb.net 2008 release vb.net has made working with xml a lot simpler, with it inclusion of XML Literals.

So you can embed and work with xml directly in your code.

Also remember the web pages can be created with xml, if you follow the xhtml specification.
Was This Post Helpful? 3
  • +
  • -

#3 Munawwar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 162
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Re: Week #37- XML

Posted 08 November 2010 - 12:04 PM

I wrote a PHP script that sorts the D.I.C C++ tutorials (using D.I.C's XML "API") based on keywords stored in an XML file - Yea, I was bored :P. The code is too long to be posted here and is quite slow in execution as it downloads 7-8 XML files.
But for a demo check this.

This post has been edited by Munawwar: 08 November 2010 - 12:04 PM

Was This Post Helpful? 2
  • +
  • -

#4 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Week #37- XML

Posted 08 November 2010 - 12:42 PM

Here's something I made using C# 4, the XDocument class and Linq. It's pretty neat and readable so anyone who wants to learn how to parse XML in a nice legible way should check it out! Better yet, help me finish it. :P

"SharpDIC is a library application made with C# that you can use to integrate any .NET application with DreamInCode using the official API. It provides common functionality right out of the box. It's free to use and anyone can contribute."

https://bitbucket.or...otapia/sharpdic
Was This Post Helpful? 1
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Week #37- XML

Posted 08 November 2010 - 02:57 PM

Does it count if I already got 2nd in the API Contest?

Anyway, here is the biggest XML project I've ever done. It reads the XML backbone of DIC, fixes some invalid XML, then parse it, then read it and turn it into Java Code Data (like C++ structs). Finally, I take those classes and put them on a GUI to be read by all you guys. I need to find a way to optimize the process, as it takes several seconds for large topics.

http://www.dreaminco...169-dicbrowser/
Was This Post Helpful? 1
  • +
  • -

#6 mocker  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 50
  • View blog
  • Posts: 466
  • Joined: 14-October 07

Re: Week #37- XML

Posted 14 November 2010 - 09:52 AM

Been a while since I've been here and figured I should do a few of these.

I posted a Javascript function I use to parse Google Buzz XML and load an HTML div with a list of recent posts. I use it as a sidebar news feed on my website
Blog post with code

*/25 * * * * wget http://buzz.googleapis.com/feeds/(Google Username)/public/posted -O /home/(username)/Site/root/static/misc/buzz.xml




$(document).ready(function(){
		loadBuzz();
});

function loadBuzz(){
	var browser = navigator.appName ;
	var xhttp = ajaxRequest();
	xhttp.open("GET","/static/misc/buzz.xml",false);
	xhttp.onerror = parseBuzzFail ;
	xhttp.send("");
	buzzDoc = xhttp.responseXML;
	if(xhttp.status == 200) { parseBuzz(); }
	else { 
		alert("Jiminy no doc- status: "+xhttp.status+" \ncontentType: "+xhttp.statusText);
	}
} 


function parseBuzz(){
	if(!buzzDoc) return;
	if(isIE && buzzDoc.readystate != 4) return;
	
	var entries = buzzDoc.getElementsByTagName("entry") ;
	var html = "<ul><li><b>Latest Buzz:</b></li>\n";
	for(var i=0; i<entries.length;i++)
	{
		html =html + "<li>";
		if( entries[i].getElementsByTagName("updated").length > 0) html += "<span class='buzzbox'><i>"+entries[i].getElementsByTagName("updated")[0].childNodes[0].nodeValue+"</i></span><br>";
		if( entries[i].getElementsByTagName("link").length > 0) html += "<a href='"+entries[i].getElementsByTagName("link")[0].getAttribute("href")+"'>";
		else html += "<a target='#'>";
		html =html + entries[i].getElementsByTagName("summary")[0].childNodes[0].nodeValue+"</a></li><li></li>\n";
	}
	html += "</ul>";
	
	$("#DV_BUZZBOX").html(html);
	
}


Was This Post Helpful? 2
  • +
  • -

Page 1 of 1