php xml parsing in several ways

php parsed xml offers several classes or methods, comprising: Xml parser, SimpleXML ,, XMLReader ,, DOMDocument.

 

XML Expat Parser:

Use XML Parser Expat XML parser. Expat parser is an event-based, it is an XML document as a series of events. When an event occurs, it calls a specified function to handle it. Expat is a non-validating parser, ignoring any link to the document's DTD. (Recommended Learning: PHP Programming from entry to the master)

However, if the document is not good form, it will end with an error message. Because it is based on an event, and no verification, Expat has the characteristics and is suitable for rapid web application.

The advantage of XML Parser is a good performance, because it is not the entire xml document into memory and then deal with, but side edge analytical processing. But precisely because of this, it is not suitable for those needs to be done dynamically adjusted xml structure, or do complex operations based on xml context structure.

If you just want to deal with a well-structured analytical xml document, so it can complete the task well. Note that the XML Parser supports only three encoding formats: US-ASCII, ISO-8859-1 and UTF-8, if your other xml data is encoded, you need to convert into one of the above three.

XML Parser commonly used analytical methods in general, there are two (in fact, two functions): xml_parse_into_struct and xml_set_element_handler.

xml_parse_into_struct

This method is to parse the data into two arrays xml:

Array index - points to a location containing the value of the Value of the pointer array

value array - containing data from the parsed XML

SimpleXML

SimpleXML is a user-friendly tool set after xml PHP5 provided, can be converted into convenient process xml objects can also be organized to generate xml data. But it does not apply to xml namespace contains, but also to ensure the full xml format (well-formed).

It provides three methods: simplexml_import_dom, simplexml_load_file, simplexml_load_string, the function name is a visual depiction of the action function. Three SimpleXMLElement object function returns, the data read / add operation is through SimpleXMLElement.

SimpleXML advantage is to develop simple, the disadvantage is that it will be loaded into memory before the whole xml processing, so when parsing xml content over many documents may be inadequate. If you are reading small files, but does not contain the xml namespace, that SimpleXML is a good choice.

XMLReader

XMLReader is extended after PHP5 (after 5.1 installed by default), it is like moving a cursor in the document flow, and stop at each node, the operation is very flexible. It provides a quick and non-cached streaming access to inputs, you can read the stream or document, allowing users to extract data, and skip the record does not make sense for the application.

Use DomDocument parse XML file

Steps:

1, create a node using the createElement method,

2, create a text content using createTextNode method,

3, add child nodes appendChild method,

4. Create a property using the method createAttribute

Guess you like

Origin www.cnblogs.com/68xi/p/11528023.html