Quick Snippets: PHP XML Parsing

August 29th, 2006 Comments Off

Like many, I am limited to PHP 4 on most of my web servers, that until Plesk and SWSoft decide to support PHP 5. PHP 5 has advanced a lot in XML parsing, so I am looking forward to getting into it. So this XML parsing class is on that I have used for the last several years. It’s really easy to use, and simple for just about any situation.

The Class

[php]
class CXMLParser
{
// Variables
var $m_aData;
var $m_aStack;

// Class creation
function CXMLParser()
{
$this->m_aData = array();
$this->m_aStack = array();
}

// Parse
function parse($strData)
{
// Declare variables
$xmlParser = xml_parser_create();

// Set parser to this
xml_set_object($xmlParser, $this);

// Parse XML
xml_set_element_handler($xmlParser, “XML_StartElement”, “XML_EndElement”);
xml_set_character_data_handler($xmlParser, “XML_CharacterData”);
if (xml_parse($xmlParser, $strData, true) == false)
{
return false;
}

// Free parser
xml_parser_free($xmlParser);

return true;
}

// Get data
function getData()
{
return $this->m_aData;
}

function &XML_CurrElement()
{
$aCurr = &$this->m_aData;

foreach ($this->m_aStack as $strNode)
{
$strTemp = & $aCurr[$strNode];
$aCurr = & $strTemp;
}

return $aCurr;
}

function XML_AddNode($strName)
{
$aCurr = &$this->XML_CurrElement();
if (!array_key_exists($strName, $aCurr))
{
$aCurr[$strName] = array();
}
}

function XML_StartElement($xmlParser, $strName, $aAttrb)
{

$this->XML_AddNode($strName);
$this->m_aStack[] = $strName;
}

function XML_EndElement($xmlParser, $strName)
{

$aPopped = array_pop($this->m_aStack);
assert(‘($aPopped == $strName)’);
}

function XML_CharacterData($xmlParser, $aData)
{
$aCurr = &$this->XML_CurrElement();

if (isset($aCurr["_data"]))
{
$aCurr["_data"] .= $aData;
}
else
{
$aCurr["_data"] = $aData;
}
}
}
?>
[/php]

Usage:

[php]
$cXMLParser = new CXMLParser();
if ($cXMLParser->parse(“1“))
{
$aAPIData = $cXMLParser->getData();
die($aAPIData["DATA"]["VALUE"]["_data"]);
}
[/php]

Be Prepared for Your Java Interview

August 25th, 2006 Comments Off

Interviews can be a stressful situation, especially those in the technology industry. Some technology companies are known asking questions like “Why are Manhole Covers Round?” a question made famous by Tony Martinez from Microsoft and meant as a psychological assessment of how one approaches a question with more than one “right” answer. I always enjoy reading up on interview questions and answers, because aside from it preparing you for an interview, I feel they help you think about topics differently in some cases. AllAppsLabs.com has a series of very good interview questions for a Java position. It’s a good run down of some of the more common questions you may see if a Java interview.

BlueCrestStudios Readies v1.0.4.0 of BackupSQL Studio

August 24th, 2006 Comments Off

BackupSQL Studio v1.0.4.0 is in its final stages of development and testing. This release includes better support for MySQL table structure dumping, along with other minor bug fixes. The biggest improvement with this version is the functionality of backing up databases with a PHP script.

We have also started development for SQL Server and PostgreSQL support, however this support will not be in the v1.0.4.0 release.

Please keep on the look out for the new version. We are very excited about v1.0.4.0 of BackupSQL Studio.

Convert CVS Respository to Subversion

August 24th, 2006 Comments Off

A while back I posted about my transition to Subversion from Perforce, and I must say that I am 100% happy in my switch. One of the things that I looked into was how to convert a Perforce repository to Subversion, I was unsuccessful in my venture but will hopefully prevail one day soon.

I recently came across a great tutorial from my new favorite site juretta.com on how to convert a CVS repository to Subversion. In two easy steps they outline what you need to do do accomplish this transition. And for anyone who hasn’t made the switch, please do… you will not regret it.

Bug-Track.com version 3.1

August 22nd, 2006 Comments Off


Bug-Track.com’s latest version improves productivity by adding Really Simple Syndication (RSS) feeds to projects. It’s now easier than ever to monitor a bug pool for incoming bugs.

A new Dashboard view provides powerful visualisation of bug information to help detect and understand trends. The addition of a client management feature lets users specify which clients are affected by a particular bug. Client specific bug reports can then be generated.

The development of a new configuration tool and support for Microsoft SQL Server 2005â„¢ simplifies the deployment of Bug-Track.com.

For easier archiving of old bugs, lookup values can now be made obsolete to prevent them from being used when creating new bugs.

Individual users, via their profile, now have the ability to control which page to display when the start the Bug-Track.com application.

Finally, it’s now possible to edit saved queries used for searches and the new dashboard.

To view all features, go to: http://www.bug-track.com/main/bugtrackingfeatures.jsp

Best-of-breed companies in different industries, ranging from General Electric and Virgin to the US Banking Alliance, America West Airlines and Admiral Secure Products, have selected Bug-Track.com for their project management needs.

For more information concerning Bug-Track.com, contact them at: sales@bug-track.com or http://www.bug-track.com

Quick Snippets: Copy a File with Java (Revision 2)

August 22nd, 2006 § 11

Revision v2

Per feedback:

  • Close streams in finally block
  • Source and destination parameters are now of type File instead of String
  • Function is void, and throws IOException if error

—-

Quick snippets are posted live, this little ditty was written all of ten minutes ago. I needed a quick function to copy one file to another location in a small Java project I have going. Java does have a File.renameTo function that can be used to move a file, so to copy we have to actually read and write the file to the new location. This quick snippet can be included and used globally, in addition the buffer size can be changed for better performance.

[java]

// Copy file
public static void copyFile(File fSource, File fDest) throws IOException
{
// Declare variables
InputStream sIn = null;
OutputStream sOut = null;

try
{
// Declare variables
int nLen = 0;
sIn = new FileInputStream(fSource);
sOut = new FileOutputStream(fDest);

// Transfer bytes from in to out
byte[] bBuffer = new byte[1024];
while ((nLen = sIn.read(bBuffer)) > 0)
{
sOut.write(bBuffer, 0, nLen);
}

// Flush
sOut.flush();
}
finally
{
// Close streams
try
{
if (sIn != null)
sIn.close();
if (sOut != null)
sOut.close();
}
catch (IOException eError)
{
}
}
}

[/java]

Tab or Spaces?

August 22nd, 2006 § 8

Code syntax has long been a debate amongst developers. Tabs or spaces, bracket on newline or same line, comment colors, Hungarian notation, and the list goes on and on.

Personally I use tabs, and usually set my IDE settings to eight spaces wide. I do not know why, it’s just what I have always done and it allows me to read code better. I am also a bracket on the newline kind of guy, I find it works better with code folding features that some IDE’s have. As far as Hungarian notation, it’s a must for me. I can’t stand one or two letter variable names; it reminds me a lot of spaghetti code. One of the great features that some IDE’s have is code reformatting, where with the click of a button you can reformat code to your liking. However, this sometimes throws off source code control and angers other developers. So I usually do it in a temporary file if I absolutely can’t read a thing.

What are your code syntax pet peeves? What is your coding style?

What is it With the Java Haters?

August 17th, 2006 § 20

So this isn’t a rant, it really is an effort to understand why people are so wrong in their assumptions about Java. I follow a lot of Java blogs, forums, newsletters; and sure enough every other day you have some one commenting something along the line of “Java is Dead” and continue to say Ruby is the new king. The thing I do not understand is Ruby and PHP can’t do desktop applications, and a lot of companies, even Fortune 500 companies, use Java on the desktop because of it works on multiple OS’s. In addition: look at your cell phone, Java is used on a lot of cell phones for the same reason.

I understand that there is a lot of buzz around the Internet right now, and that software as a service is the new thing coming down the pipes; but people who make comments like this really just have me wondering what they are thinking. Yes, Ruby and PHP may have passed Java in the web market (I need to check the recent statistics), but that certainly does not mean the language is dead.

When I had some product ideas and decided to release them under BlueCrestStudios, I decided to write them in Java. I made the decision based on several reasons:

1) I have written a lot of Windows applications, and developing in Java was a joy. It’s really simple to do things, especially GUI development. Tools like NetBeans make it a joy.
2) There is a lot of buzz around other OS’s, like Apple’s OS X and Ubuntu, and more and more people are discovering that Windows isn’t the only OS out there.
3) The Java community is very much alive, despite what people think. There are new products, tools, resources coming out daily; and a lot of them are free or have a low cost.

I am not saying that Java is the end all solution, what I am saying is that it is very much alive. It’s really frustrating when I hear comments like “Java is dead, and Ruby is king”. I really just want to understand where they are coming from with a statement like that. I guess the fact that they don’t say anything else means they are a fan boy, and I guess there is nothing wrong with that just let me know why you think that… I am interested to know.

Development Tools: What Do You Use?

August 16th, 2006 § 14

Development tools come in all shapes and sizes, free and commercial. While some might call them cheating, I say if it makes you more productive, it’s fair game. I have tried a ton of tools, and have my favorites (or ones I am forced to use) which are listed below with my comments. I am really interested to hear what others are using, as I am always in the mood to try something new.

NetBeans

NetBeans is arguable the best open source IDE out there. It comes packed with all the needed features right out of the box. NetBeans also has a very small learning curve, so if you have used any IDE you will have no problem learning NetBeans. An excellent Java IDE!

UltraEdit

An old school favorite of mine, which I have used for many many years. In fact I have replaced Windows Notepad with UltraEdit, because it does a much better job as a text editor. Over the years I have seen UltraEdit grow and it has gotten a little fat, but they have done a good job of adding the features and keeping the program easy to use.

Zend Studio

I use Zend Studio for PHP projects. I have tried many PHP IDE’s, and found Zend Studio to be the best. It has a great auto complete feature, which other PHP IDE’s lacked. In addition, it does a really good job of controlling the environment and allow you to just code while it takes care of the rest.

Visual Studio

I use Visual Studio 2003 for work, and it is certainly not my favorite C++ IDE. It lacks a lot of features that are very common with IDE’s these days; like god syntax highlighting, or Ctrl+clicking on a key function call to go to that function declaration. A lot of these necessary feature can be added by using a third party plug in called Visual Assist X. I have no clue why Microsoft does not just purchase the company, and implement these features into Visual Studio.

WAMP Server

Yes I develop on a Windows, but host on Linux with Apache. So when developing web applications, WAMP Server does a very good job of setting up a local web server on your machine with all of the necessities like PHP, Apache, MySQL, etc. It even has plug ins that allow you to switch between PHP 4 and 5.

phpMyAdmin

A classic, not much to say about this one other than it;s great and you need it.

This list goes on for me, but these are the first that came to mind. Send m your list, and what you think about the tools. I am really interested in seeing what the majority of you out there use.

Unplugging From Civilization

August 4th, 2006 § 2

For the next week I will be on vacation, and visiting the fine state of New York… more specifically up state New York. I will have access to the web through a dial up connection, which I have not been on in years. I will continue to post as I am able, but this is my first vacation in years. So if the blog seems slow over the next week, I apologize but will return the next week recharged and ready to finish off the year. Thanks everyone!

Where am I?

You are currently viewing the archives for August, 2006 at Code and Coffee.