|A discussion of the basics of XML Parsing in PHP|
|The file that has the code in this discussion is called phpDiscussionOfXMLCode.php.|
|If you would like to see it in action, click here.|
|If you would like a copy of phpDiscussionOfXMLCode.php that generated the above XML parse click here.|
|For the XML file that is being read by the above code ("phpXMLSourceData.xml") click here. [Note that this XML file has the first two lines indented to show the XML but the rest are single line entries of the same tagged information.]|
There are more steps involved in PHP XML parsing than with the parsing dlls used available to use with ASP. This makes it a little more difficult to work with, because of your involvement with the process, but also a little more in your control over the parsing process. The following code line initiates the entire process by calling the function readTrialBalanceInfo which function will return a usable array [$TrialBalance] of information that you got from the XML file:
This process begins by the creation of the XML parser:
This is followed by the function which tells the parser the name of the functions that will handle the start and end tags for the XML file. These functions will be the ones that take capture and any other actions that you define to happen when the start tag is encountered or the end tag is encountered. In the following code part, function startElement and function endElement handle the starting and ending tags respectively. These functions are filled with whatever actions that you want them to do:
Then we need to tell the parser what function, again one that we can control what it does, will handle the data inside the tags. In the following sample code, the name of that function is: function characterData
The next line is the code that actually loads the XML file into the parser and reads 4096 bytes of the file at a time:
Finally, the parser is freed from memory and the filled global array variable, $TrialBalance is returned from this function to be used by the function that called this function:
Now, to discuss how the functions that the parser is told to use, is used in this simple XML parsing sample. The first function is the parser automatic call to the startElement everytime the parser encounters a starting tag. This function, the way this sample is designed, merely captures the name of the tag to the global variable $currentTag for use in the next discussed function.
This next function is also automatically called by the php parser and is used, in this sample, to build the array of values from the elements that we wish to capture. In this sample, we look at the tag name, captured above to $currentTag, and see which global variable we have chosen to update with the content in the tag. Here the account number is captured to $ACCTNO and the account description is captured to $ACCTDESC:
Now, its time to build up the XML array for use. We will add the current global variable information captured above, to the total array and then reset those variables for the next line capture. The timing of the array addition is the parser automatic call to the function when the ending or closing tag is encountered:
Finally, after the entire file has been read, and the array $TrialBalance now has the information that we decided to capture, we can now display that information with the function that started the whole process off (you may recognize the code snippet $TrialBalance = readTrialBalanceInfo(); from the first snippet above). Notice that we refer to the variable information two ways:
We captured the tag information (see snippet above) for the account number to the array reference "TheAccountNumber".
We captured the tag information (see snipped above) for the account description to the array reference "TheAccountDescription"
And finally, to see each "record", the ordinal reference is the first reference (ex:  expressed through the for loop x)
I hope that this simple example helps. There is a great deal more functionality for XML parsing in PHP and a lot of the XML parsing functions allow for your customization. It does not seem to offer the same recordset XML parsing found in Microsoft parsing dlls but it is definitely a good parsing engine.
Christopher Koniges, Systems Programmer (206) 954-0423 www.jazzysystems.com firstname.lastname@example.org