Tutorials to .com

Tutorials to .com » Php » Others » PHP with expat Analysis Toolkit XML

PHP with expat Analysis Toolkit XML

Print View , by: iSee ,Total views: 7 ,Word Count: 1992 ,Date: Mon, 20 Apr 2009 Time: 12:31 AM

xml is now advocating for all Web developers best friend, with the help of XML, which can easily format and display data from virtually any data source. However, dynamic content, the format of the data quality achieved is still far from ideal. The majority of Web developers will tell you, today's network is not how dynamic content can do it! The question is: "how to use xml in the end the creation of dynamic content?"

The answer is to use dynamic content to deal with the language to resolve XML, such as using php or Perl, etc. In theory, this type of programming language is the use of xml for various purposes of the. Is nothing more than some can be used to resolve more than XML toolkit. James Clark on the provision of a toolkit called expat. expat XML Toolkit using c language to resolve XML, and XML to make it easy for PHP dance.

PHP is a Web Design for the best scripting language. XML is that the Web content standards. Both jointly that they have to be more than the United States and how the United States!

The following presentation I will give the reader a simple example, through this example to explain how to use PHP to XML file is parsed for HTML. Then I will introduce some of the other XML concepts of PHP. Analysis of XML with PHP is very simple, very intuitive to operate but the details need to be explained. Once a real grasp of the essentials of the application, you will certainly be surprised at how their own do not think of them as early as the two together to come together.

PHP with XML tools such expat package, through the C++ language to resolve XML. This function sets the toolkit with Perl XML analytical function used is the same set, in addition, this tool kit-based or event-driven parser. That is to say, expat each XML tag or a new line of code as the beginning of the incident, which is the function of the trigger event. Expat installation is very simple, if you are using the Apache Web server, then PHP XML you can find a reference to page and download the installation guide.

XML with PHP to resolve the basic task is as follows: First, create an instance of XML parser. Then, the definition of the function to deal with trigger events, such as the beginning or end of labels. Subsequently, the actual meaning of the definition of the data-processing procedures. Finally, open the XML file, read file data and analytical data. Close the document after the release of XML parser.

You see, like I said, nothing of this particular operation. However, before we discuss specific examples of prior understanding of the following warning:

Expat does not test XML. This means that the XML file format as long as the right - all the elements properly nested start and closing tags without any error - it will be resolved. Expat available regardless of whether compliance with the XML file XML header or the definition of a standard reference.
Expat all the XML tags is converted to uppercase letters. If your script on the labels and other content on a mix of uppercase and lowercase letters may be wary.
PHP is set up in the magic quotes enabled by the compiler, then the complexity of the XML document will not be the correct analysis. If magic quotes is not the default setting, you will when I did not say.
Well, we now look at the examples!

Basic sample

In order to simplify the complexity of things, I omitted in the case of error checking and some other unnecessary things, of course, you can code in their own way. I assume you are familiar with PHP and its syntax is long overdue, and I will do some interpretation of XML functions. First of all, I will describe the meaning of the script process, followed by the definition of user-defined function, in fact, at these functions before quoting their code. Related Attachment: A list of the procedures shown in the complete code for the script, the script should be parsed XML document is related to the annex: Procedural list B. After processing the output results shown in Table A.

XML Articles

"Remedial XML for programmers: Basic syntax" In this first installment in a three-part series, I'll introduce you to XML and its basic syntax.

"Remedial XML: Enforcing document formats with DTDs" To enforce structure requirements for an XML document, you have to turn to one of XML's attendant technologies, data type definition (DTD).

"Remedial XML: Using XML Schema" In this article, we'll briefly touch on the shortcomings of DTDs and discuss the basics of a newer, more powerful standard: XML Schemas.

"Remedial XML: Say hello to DOM" Now it's time to put on your programmer's hat and get acquainted with Document Object Model (DOM), which provides easy access to XML documents via a tree-like set of objects.

"Remedial XML: Learning to play SAX" In this fifth installment in our Remedial XML series, I'll introduce you to the SAX API and provide some links to SAX implementations in several languages.

Analysis of Table A PHP output XML

First of all, I created an instance of XML parser:
$ parser = xml_parser_create ();

Then, I encountered the definition of the beginning and end of resolver label operation. Note "startElement" and "endElement" is a user-defined function, of course, you can according to their own preferences to them from other names, but I am But these names from the customary requirements of the standard.
xml_set_element_handler ($ parser, "startElement", "endElement");

Then I define the data manipulation. Here "characterData" is a user-defined function, the name is also customary.
xml_set_character_data_handler ($ parser, "characterData");

Now open the file to read data. You can here the preparation of error-handling code, I have omitted in case the error handling. Do not forget the beginning of the definition in the script $ xml_file.
$ filehandler = fopen ($ xml_file, "r");

I began to read the contents of the paper and read on 4K-byte variable "$ data" within the document until the end. I used to resolve xml_parse paragraph to read these data.
while ($ data = fread ($ filehandler, 4096)) (
xml_parse ($ parser, $ data, feof ($ filehandler));

Empty the end to close the parser, such as the release of documents and operations.
fclose ($ filehandler);
xml_parser_free ($ parser);

The above script is used by all the XML functions, following a detailed explanation for me to use one of three user-defined function, which is "startElement", "endElement" and "characterData".

Xml_parse as long as the beginning of this encounter <url> label, "startElement" function call by the XML parser, in our example is the parser $ parser. The function is a function must be defined, it has automatic three parameters passed to it: XML parser examples the elements of the name of capital, such as the URL and the elements have the properties of the array. In the example above, XML documents do not attribute a collection of elements, the array parameter is empty, but this parameter must exist.

On this case, I have decided in a HTML table to display my XML data. As indicated above, for reasons I do not have to simplify the preparation of error-handling code. I played a cover-up, because I know there XML document, the order of labels. Otherwise, I can use "startElement", "characterData" and "endElement" function to define the array, and then show the results of separate functions.

function startElement ($ parser_instance, $ element_name, $ attrs) (
switch ($ element_name) (
case "URL": echo "<tr> <td> <a href =" ";
case "SUMMARY": echo "<td>";

After dealing with the element tags, xml_parse Once XML data is called "characterData" function. This function is automatically called by the parser, this function requires two parameters, resolver, and contains examples of data strings.
function characterData ($ parser_instance, $ xml_data) (
echo $ xml_data;

Xml_parse encountered the end of the last label, run "endElement" function, the function with two parameters, examples and elements of the name resolver.
function endElement ($ parser_instance, $ element_name) (
switch ($ element_name) (
case "URL": echo "">";
case "TITLE": echo "</ a> </ td>";
case "SUMMARY": echo "</ td> </ tr>";

XML with PHP to resolve the light-weight training to the end here, following some hard work we begin to increase.

Increasing function

There are a number of other PHP and XML-related function analysis. PHP.net documentation of these functions a complete description. I would like to mention some, you may soon use these functions:

xml_set_default_handler () - the function of the working methods and xml_set_character_data_handler () function is similar to, but it is defined to capture all. This function commonly used in the document type declaration to control data processing.
xml_parser_set_option () - you can use this function to disable the switch to capital letters or alternative operation of the character encoding set.
xml_parse_into_struct () - This function used to bypass the "startElement", "characterData" and "endElement" function call, and the data directly into the array.
xml_error_string () - This function is used from xml_parser () error in the access to text messaging.
xml_get_error_code () - you can use this function to get the above-mentioned error string. The use of the latter two functions like this: if (! Xml_parse ($ parser, $ data, feof ($ filehandler))) (die (xml_error_string (xml_get_error_code ($ parser));)
If you feel that they have started, I suggest that you carefully read the PHP manual page provided by XML External Entity Example. The examples in this article made no mention of a number of other concepts as well as some technical error.

In this paper, PHP and XML demonstrate the close integration of the process. Two types of technology to Web-centric nature to their dynamic content together to become the ideal solution.

php other Articles

Can't Find What You're Looking For?

Rating: Not yet rated


No comments posted.