How to use php to parse xml files. DOMElementDOMElementDOMDocument: createElement (string $ name [, string $ value]) create a node element String $ name: node name String $ value: node value 8. add node DOMN DOMElement
DOMElement DOMDocument: createElement (string $ name [, string $ value])
Create node element
String $ name: node name
String $ value: node value
8. add nodes
• DOMNode: appendChild (DOMNode $ newnode)
Add a subnode
DOMNode $ newnode: new node
In dom operations, the addition, deletion, modification, and deletion operations must depend on the parent node.
9. Save
• String DOMDocument: saveXML
Save to a string
• Int DOMDocument: save (string $ filename)
Save to an object
String $ filename: file name
10. delete a node
• DOMNode: removeChild (DOMNode $ oldnode)
Delete a node
DOMNode $ oldnode: the node to be deleted
11. update nodes
• DOMNode: replaceChild (DOMNode $ newnode, DOMNode $ oldnode)
DOMNode $ newnode: new node
DOMNode $ oldnode: original node
12. add attributes
• DOMAttr DOMElement: setAttribute (string $ name, string $ value)
String $ name: attribute name
String $ value: attribute value
13. modify attributes
DOMAttr DOMElement: setAttribute (string $ name, string $ value)
String $ name: attribute name
String $ value: attribute value
14. delete attributes
• Bool DOMElement: removeAttribute (string $ name)
String $ name: name of the attribute to be deleted
15. get attributes
• String DOMElement: getAttribute (string $ name)
String $ name: attribute name of the property value to be obtained
DOMDocument is part of the DOM extension launched after PHP5 and can be used to establish or parse html/xml. Currently, only UTF-8 encoding is supported.
| The code is as follows: |
|
|
$ Xmlstring = <
Login Imdonkey
XML;
$ Dom = new DOMDocument ();
$ Dom-> loadXML ($ xmlstring );
Print_r (getArray ($ dom-> documentElement ));
Function getArray ($ node ){
$ Array = false;
If ($ node-> hasAttributes ()){
Foreach ($ node-> attributes as $ attr ){
$ Array [$ attr-> nodeName] = $ attr-> nodeValue;
}
}
If ($ node-> hasChildNodes ()){
If ($ node-> childNodes-> length = 1 ){
$ Array [$ node-> firstChild-> nodeName] = getArray ($ node-> firstChild );
} Else {
Foreach ($ node-> childNodes as $ childNode ){
If ($ childNode-> nodeType! = XML_TEXT_NODE ){
$ Array [$ childNode-> nodeName] [] = getArray ($ childNode );
}
}
}
} Else {
Return $ node-> nodeValue;
}
Return $ array;
} |
SimpleXML
SimpleXML is a simple and easy-to-use xml tool set provided by PHP5. it can convert xml into easy-to-process objects and organize and generate xml data. However, it does not apply to xml that contains namespace, and must ensure that the xml format is complete (well-formed ). It provides three methods: simplexml_import_dom, simplexml_load_file, and simplexml_load_string. the function name intuitively illustrates the function's role. All three functions return SimpleXMLElement objects, and data reading/adding is performed through SimpleXMLElement.
| The code is as follows: |
|
|
$ String = <
Login Imdonkey
XML;
$ Xml = simplexml_load_string ($ string );
Print_r ($ xml );
$ Login = $ xml-> login; // The Returned result is still a SimpleXMLElement object.
Print_r ($ login );
$ Login = (string) $ xml-> login; // when comparing data, be sure to forcibly convert
Print_r ($ login ); |
SimpleXML is easy to develop. Its disadvantage is that it loads the entire xml into the memory before processing. Therefore, it may not be able to resolve xml documents with too much content. If you want to read small files and xml does not contain namespace, SimpleXML is a good choice.
XMLReader
XMLReader is also an extension after PHP5 (installed by default after 5.1). It moves in the document stream just like a cursor and stops at each node, making the operation flexible. It provides fast and non-cache stream access to the input, and can read streams or documents, allowing users to extract data from it and skip records that are meaningless to the application.
The following uses the google Weather api to obtain information. XMLReader only involves a small number of functions. For more information, see the official documentation.
| The code is as follows: |
|
|
$ Xml_uri = 'http: // www.google.com/ig/api? Weather = Beijing & hl = zh-cn ';
$ Current = array ();
$ Forecast = array ();
$ Reader = new XMLReader ();
$ Reader-> open ($ xml_uri, 'gbk ');
While ($ reader-> read ()){
// Get current data
If ($ reader-> name = "current_conditions" & $ reader-> nodeType = XMLReader: ELEMENT ){
While ($ reader-> read () & $ reader-> name! = "Current_conditions "){
$ Name = $ reader-> name;
$ Value = $ reader-> getAttribute ('data ');
$ Current [$ name] = $ value;
}
}
// Get forecast data
If ($ reader-> name = "forecast_conditions" & $ reader-> nodeType = XMLReader: ELEMENT ){
$ Sub_forecast = array ();
While ($ reader-> read () & $ reader-> name! = "Forecast_conditions "){
$ Name = $ reader-> name;
$ Value = $ reader-> getAttribute ('data ');
$ Sub_forecast [$ name] = $ value;
}
$ Forecast [] = $ sub_forecast;
}
}
$ Reader-> close (); |
Similar to XML Parser, XMLReader and XML Parser are both read-side operations. The big difference is that the SAX model is a "push" model, where the analyzer pushes events to the application, the application is notified every time a new node is read. the XmlReader application can extract nodes from the reader at will, which is more controllable.
Because XMLReader is based on libxml, you need to refer to the documentation to see if it is applicable to your libxml version.
Http://www.bkjia.com/PHPjc/372026.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/372026.htmlTechArticleDOMElement DOMElement DOMDocument: createElement (string $ name [, string $ value]) create a node element String $ name: node name String $ value: node value 8. add node DOMN...