XML overview
xml represents the abbreviation for Extensible Markup Language (extensible Markup, meaning Extensible Markup Language). XML is a set of rules that define semantic markup that divides documents into many parts and identifies them. It is also a Meta markup language, defined as a syntactic language for defining other semantic, structured markup languages related to a particular domain. XML is the hottest technology today. and PHP also has the ability to analyze XML documents, we will discuss the application of XML in PHP together.
xml Overview
Talking about XML (eXtended Markup Language: Extensible Markup Language), let's look at a piece of HTML code first:
<title>xml</title>
<body>
<p><center><font color= "Red" >TEXT</font></center></p>
<a href= "www.domain.com" ></a>
</body>
The above code is structurally compliant with the XML rules, and XML can be understood to be a tree-like structure type that contains data:
1, referencing the same element, using a consistent case, such as <center></Center> is not in line with the rules
2, any property values (such as href= "????") To be caused by "", such as <a href=www.yahoo.com> is incorrect.
3, all elements must consist of open < and close > annotations, elements should be shaped like <body></body> or empty elements 4, all elements must be nested with each other, just like the loop of writing a program, and all elements must be nested within the root element, such as the above code, all of which are nested within 5, the element name (that is, the body a P IMG, etc.) should begin with a letter.
How to apply PHP XML parser expat?
expat is an XML parser (also known as an XML processor) in the PHP scripting language that enables programs to access the structure and content of XML documents. It is an event-based parser. There are two basic types of XML parsers:
A tree-based parser: Converts an XML document into a tree-like structure. This type of parser analyzes the entire article and provides an API to access each element of the resulting tree. Its common standard is DOM (Document object mode).
event-based Parser: Treats an XML document as a series of events. When a particular event occurs, the parser will invoke the function provided by the developer to handle it. An event-based parser has a view of the dataset in an XML document, which means it is concentrated in the data portion of the XML document, not its structure. These parsers process the document from start to finish and report to the application similar to the beginning of the element, the end of the element, the beginning of the feature data, and so on-events through the callback (callback) function.
The following is an example of an "Hello-world" XML document:
<greeting>
Hello World
</greeting>
The event-based parser will report as three events:
Start element: Greeting
The beginning of the cdata entry, the value is: Hello World
End element: Greeting
An event-based parser does not produce a structure that describes the document, and of course if you use expat, it can generate a complete native tree structure in PHP as necessary. In a CDATA item, an event-based parser does not get information about the greeting of the parent element. However, it provides a lower level of access, which makes it possible to make better use of resources and faster access. In this way, there is no need to put the entire document into memory, and in fact the entire document can even be larger than the actual memory value.
The example above Hello-world includes the full XML format, but it is not valid because there is neither a DTD (document type definition) associated with it nor an inline DTD. However, expat is a parser that does not check for validity, and therefore ignores any DTD associated with the document. It should be noted that the document still needs the full format, otherwise expat (like any other XML-compliant parser) will stop with the error message.
Compile expat
Expat can be compiled into the PHP3.0.6 version (or more). Starting with Apache1.3.22, expat has been part of Apache. In UNIX systems, you can configure PHP to compile it into PHP using the-with-xml option.
If you compile PHP as an Apache module, expat will default as part of Apache. In Windows, you must load the XML dynamic connection library.
XML Example: Xmlstats
The example we are going to discuss is using expat to collect statistics for XML documents.
For each element in the document, the following information is output:
The number of times the element is used in the document
The number of character data in the element
Element's parent element
Child elements of an element
Note: In order to demonstrate, we use PHP to produce a structure to hold the parent element and child element of the element.
What are the functions that uses to produce an instance of an XML parser?
The function used to produce an instance of an XML parser is xml_parser_create (). The instance will be used for all future functions. This idea is very similar to the connection mark of MySQL function in PHP. An event-based parser typically requires a registration callback function before parsing a document-for a particular event to occur. Expat no exceptions, it defines the following seven possible events:
Object XML parsing function description
Start and end of element Xml_set_element_handler () element
Start of character data Xml_set_character_data_handler () character data
External entity Xml_set_external_entity_ref_handler () external entity appears
unresolved external entity xml_set_unparsed_entity_decl_handler () unresolved external entities appear
Processing instruction Xml_set_processing_instruction_handler () the appearance of processing instructions
The appearance of the Declaration of Xml_set_notation_decl_handler () notation of notation
Default Xml_set_default_handler () other events that do not have a handler function specified
All callback functions must have an instance of the parser as its first argument (in addition to other parameters).
For the example script at the end of this article, it is necessary to note that it uses both the element processing function and the character data processing function. The callback handler function for the element is registered by Xml_set_element_handler ().
This function requires three parameters:
Instance of the parser
Name of the callback function that handles the start element
Name of the callback function that handles the end element
When you begin parsing an XML document, the callback function must exist. They must be defined as consistent with the prototype described in the PHP manual.
For example, expat passes three arguments to the handler function of the start element. In the scripting example, it is defined as follows:
function start_element ($parser, $name, $attrs)
$parser is the parser flag, $name is the name of the start element, $attrs is an array containing all the attributes and values of the element.
Once the XML document is parsed, expat will invoke the Start_element () function and pass the arguments to the previous element when it encounters the start elements.
Case folding options FOR XML
Close the case folding option with the Xml_parser_set_option () function. This option is turned on by default, so that the element name passed to the handler is automatically converted to uppercase. However, XML is sensitive to capitalization (so capitalization is very important for statistical XML documents). For our example, the case folding option must be closed.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.