Jump to content
  • Advertisement
Sign in to follow this  
Drew_Benton

Unity [web] XML, C#, and PHP Fun

This topic is 4884 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So right now I am learning C# and since I can get free hosting with PHP support [wink], I had the idea of learning some PHP on the side as well now that I don't have to setup my own local php server. So, to start off I found this neat little tutorial on using XML with PHP. Reason being is that I am using C# right now for web data parsing and converting into XML. Starting off, after going though that tutorial, I decided to make my own version, one that would parse the XML files in my format from my C# program. It was no easy task! So after hours of long struggling [lol], this is what it ends up looking like. I know I still have some work left to do on the formatting of that table. My original XML file is right here as well. Now I have always been an advocate of data driven programs. So the reason for this post is to share a program I made that will make life 100x easier when you want to do this. Rather than have to go though and hand convert that tutorial into the format you need, this little program will do it for you! It works rather simple. You first make your XML file: Users.xml -
<Root>
	<User>
		<Member_Name>Null and Void</Member_Name>
		<Date_Joined>6/8/2000</Date_Joined>
	</User>
	<User>
		<Member_Name>Drew_Benton</Member_Name>
		<Date_Joined>7/29/2004</Date_Joined>
	</User>
	<User>
		<Member_Name>Zynadd</Member_Name>
		<Date_Joined>5/26/2005</Date_Joined>
	</User>
</Root>
that you want to use as the data source (click link for test example). Next you have to make two files: HTML_Settings.txt -
Member Name
Date Joined
and PHP_Settings.txt -
Member_Name
Date_Joined
to the program can get the format of how the PHP file will be generated. Finally, you call the program using the command line:
Generator.exe [Php file to be generated] [XML file to use] [XML file seperator]

Generator.exe MyTest.php Users.xml User
And the final PHP file will be generated: MyTest.php -
<?php

// The variables to store the data from each element
$Member_Name = "";
$Date_Joined = "";

// Holds the name of the current element
$currentElement = "";

//array to hold all the book data
$items = array();

/*	The start Element Handler
*	This is where we store the element name, currently being parsed, in $currentElement.
*	This is also where we get the attribute, if any.
*/
function startElement( $parser, $name, $attr )
{
	$GLOBALS['currentElement'] = $name;
}

// The end Element Handler
function endElement( $parser, $name )
{
	$elements = array( 'Member_Name', 'Date_Joined' );
	if( strcmp( $name, "User") == 0 )
	{
		foreach($elements as $element)
		{
			$temp[$element] = $GLOBALS[$element];
		}
		$GLOBALS['items'][]=$temp;

		// After parsing a book we reset the rest of the globals.
		if( strcmp( $name, "User") == 0 )
		{
			$GLOBALS['Member_Name'] = "";
			$GLOBALS['Date_Joined'] = "";
		}
	}
}

/*	The character data Handler
*	Depending on what the currentElement is, 
*	the handler assigns the value to the appropriate variable
*/
function characterData( $parser, $data )
{
	global $currentElement;
	$elements = array( 'Member_Name', 'Date_Joined' );

	foreach ($elements as $element) 
	{
		if ($GLOBALS["currentElement"] == $element)
		{
			$GLOBALS[$element] .= $data;
		}
	}
}

/*	This is where the actual parsing is going on.
*	parseFile() parses the xml document and return an array
*	with the data we asked for.
*/
function parseFile()
{
	global $items;

	// Create the xml parser
	$xml_parser = xml_parser_create();

	/*Register the handlers*/
	xml_set_element_handler( $xml_parser, "startElement", "endElement" );
	xml_set_character_data_handler( $xml_parser, "characterData" );

	/*Disables case-folding. Needed for this example*/
	xml_parser_set_option( $xml_parser, XML_OPTION_CASE_FOLDING, false );

	/*Open the xml file and feed it to the parser in 4k blocks*/
	if( !( $fp = fopen( "Users.xml", "r" ) ) )
	{
		die("Cannot open: Xml file");
	}

	while( ( $data = fread( $fp, 4096 ) ) )
	{
		if( !xml_parse( $xml_parser, $data, feof( $fp ) ) )
		{
			die( sprintf("XML error at line %d column %d ", xml_get_current_line_number( $xml_parser ), xml_get_current_column_number( $xml_parser ) ) );
		}
	}

	// Finish ! we free the parser and returns the array
	xml_parser_free( $xml_parser );

	return $items;
}

/***************************************************************************************************
Calling the parseFile() and getting the result out in a simple html-table
***************************************************************************************************/
$temp = parseFile();

print '
	<table border="1">';
print '
	<tr>
		<td colspan="3">
			<b>Member Name</b>
		</td>

		<td colspan="3">
			<b>Date Joined</b>
		</td>

	</tr>
	';

foreach($temp as $arr)
{
	print '
	<tr>
		<td colspan="3">
			'.$arr["Member_Name"].'
		</td>

		<td colspan="3">
			'.$arr["Date_Joined"].'
		</td>

	<tr>
';
}
print '</table>';
?>




You just move your PHP file and XML file into your webspace then when you go to access it, you should have a nice working simple table [smile] Ok coding wise, that was not all that fun. I don't know if anyone has tried writing code that writes code, but it's hard to debug! I will be making a different post on the C# part in the .Net forums later on. Just wanted to share this concept in the Web forums for any suggesions or ideas on this matter - XML and PHP bindings and what not. Thanks for your time! Oh the demo can be downloaded here. [Edited by - Drew_Benton on June 13, 2005 1:52:41 PM]

Share this post


Link to post
Share on other sites
Advertisement
It might be better to use a MySQL database and generate it on the fly as it can be a hassle to open a file and add in the users manually. That's just my thoughts though. But now you realize the power of PHP and C# [smile].

Share this post


Link to post
Share on other sites
Thanks for the suggestion guys [smile]. Learning how to use MySQL is still on my Evergrowing ToDo List™ Rob, so I will eventually take a look at that. Actually the C# part of my program takes care of making the XML file. I enter in an ISBN number and it generates all that book information from the web. As for XSLT, that looks very interesting. I too will take a look at it down the line, but right now I like the very simple aspect of files and loading them. [wink] I know down the line as things get 'bigger', another solution is needed though.

[Edited by - Drew_Benton on June 13, 2005 1:54:50 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!