XML application in PHP (1)

xiaoxiao2021-03-06  80

XML application in PHP (1)

Source: Aspsky                                                                                                         . XML is a set of rules that define semantic tags that divide documents into many parts and identify them. It is also a meta-marking language that defines syntax language for defining other, semantic, structured marking languages ​​related to specific areas. XML is the most popular technology today. PHP also has the function of analyzing the XML document. Here we will discuss the situation of XML applications in PHP.                                P>

text The above code is in line with XML rules from the structure, and XML can understand the structure type of the tree containing data:           When the same element is used, the consistent case is used, such as
is not in line with the specified   2, any property value (such as href = / "???? /") Use / "/", such as labels, elements should be like Or empty element , if the end / "/>" less / "//" is the wrong code   4, all the elements must nest each other, just like the loop of the writer, Moreover, all elements must be nested in root elements, such as all contents of the above code are nested in . 5, the name of the element (i.e., the Body a P IMG, etc.) should begin with letters. How to apply PHP XML parser EXPAT? Expat is the XML parser of the PHP script language (also known as an XML processor) that allows the program to access the structure and content of the XML document. It is an event-based parser. The XML parser has two basic types:   Based on the tree-type parser: convert the XML document into a tree structure. Such parsers analyze the entire article while providing an API to access each element of the generated tree. Its generic standard is DOM (document object mode). Event-based parser: treating XML documents as a series of events. When a special event occurs, the parser handles the function provided by the developer. Event-based parser has an XML document data set, that is, it focuses on the data part of the XML document, not its structure. These parsers process documents from head to tail, and will similar to the beginning of the element, the end of the element, the start of feature data, etc. - Event Reports to the application via the callback function.

The following is a / "Hello-World /" XML document example: Hello World Event-based parser will report as three events:   start element: greeting   cata item The value is: hello world   end element: Greeting event-based parser does not produce the structure of the document, of course, if necessary, it can generate a complete native tree structure in the PHP if necessary. In CDATA items, event-based parsers do not get information of the parent element Greeting. However, it provides a lower-level access, which makes it better to use resources and access faster. In this way, there is no need to put the entire document into memory; in fact, the entire document can even be greater than the actual memory value. The above Hello-World example though includes a complete XML format, but it is invalid because there is neither a DTD (document type definition), and there is no embedded DTD. But EXPAT is a parser that does not check the validity, so I ignore any DTD contact with the document. It should be noted that the document still needs a complete format, otherwise expat (and other parsers that meet the XML standard) will stop with the error information. Compiling EXPAT EXPATs can be compiled into the PHP3.0.6 version (or more). Starting from Apache 1.3.22, Expat has been part of Apache. In the UNIX system, PHP can be configured to configure PHP through the -with-xml option. If PHP is compiled into a module of Apache, the expat will be part of Apache by default. In Windows, you must load an XML dynamic connection library. XML Example: Xmlstats We want to discuss the examples of using Expat to collect statistics for the XML document. For each element in the document, the following information will be output:

The elements used in the document of the sub-elements of the parent element of the number of characters in the element data Note: For demonstration, we use php to generate a structure to save the parent element and child elements of the element.  Used to generate XML parsing What are the functions of an instance? A function for generating an XML parser instance is XML_Parser_create (). This instance will be used for all functions. This idea is very similar to the connection tag of the MySQL function in PHP. Event-based parsers are usually required to register the callback function before resolving the document - calling when a specific event occurs. Expat no exceptions, it defines the following seven possible events: the beginning and end of the character objects XML parsing function describes the elements xml_set_element_handler () element data xml_set_character_data_handler () to start an external entity xml_set_external_entity_ref_handler character data () external entities appear External unparsed entity xml_set_unparsed_entity_decl_handler () Unresolved external entity appearance processing command XML_SET_PROCESSING_INSTRUCTION_HANDLER () Processing Declaration of XML_SET_NOTATION_DECL_HANDLER () Declaration Default XML_SET_DEFAULT_HANDLER () Other Events without specifying a handler   All callback functions must be parsing Examples are the first parameter (there are other parameters). For the final example script of this article, it is important to note that it uses both the element processing function and the character data processing function. The callback process function of the element is registered by XML_SET_ELEMENT_HANDLER (). This function requires three parameters: The name of the callback function of the parser's instance processing start element The name of the callback function of the end element must exist when the XML document begins. They must be defined as the same as the prototypes described in the PHP manual. For example, EXPAT passes three parameters to the process function of the start element. In the script example, it is as defined as follows: Function Start_Element ($ PARSER, $ Name, $ Attrs)   $ parser is a parser flag, $ name is the name of the start element, $ attrs is all attributes and values ​​including elements Array. Once the XML document is started, EXPAT will call the start_element () function and pass the parameters in the past. XML's Case Folding options are turned off with an XML_Parser_Set_Option () function to turn the Case Folding option. This option is open by default, so that the element name passed to the processing function is automatically converted to uppercase. But XML is sensitive to uppercase (so case in cases are very important to statistical XML documents). For our example, the CASE Folding option must be turned off.


New Post(0)