Correct use of PHPXMLReader to parse XML documents. The sample code using PHPXMLReader is as follows :? PHPheader (Content-type: texthtml; Charsetutf-8); $ urlwww. google. comigapi? Weathershenzhen; we are using XML content loading.
The code example of PHP XMLReader is as follows:
- <? PHP
- Header ("Content-type: text/html;
Charset = utf-8 ");
- $ Url = "http://www.google.com/
Ig/api? Weather = shenzhen ";
- // Load XML content
- $ Xml = new XMLReader ();
- $ Xml-> open ($ url );
- $ Condition = '';
- $ Temp_c = '';
- While ($ xml-> read ()){
- // Echo $ xml-> name, "=> ",
$ Xml-> depth ,"
";
- If (! Empty ($ condition)
&&! Empty ($ temp_c )){
- Break;
- }
- If ($ xml-> name = 'condition'
& Amp; empty ($ condition )){
- // Obtain the first condition
- $ Condition = $ xml-> getAttribute ('data ');
- }
- If ($ xml-> name = 'temp _ c '&&
Empty ($ temp_c )){
- // Obtain the first temp_c
- $ Temp_c = $ xml-> getAttribute ('data ');
- }
- $ Xml-> read ();
- }
- $ Xml-> close ();
- Echo 'weather: ', $ condition,' <br/> ';
- Echo 'temperature: ', $ temp_c,' <br/> ';
We just need to use PHP XMLReader to get the first condition and the first temp_c, so we traverse all the nodes, write the first condition and the first temp_c into the variable, and finally output the result.
The following is an example of the PHP XMLReader code :? PHP header (Content-type: text/html; Charset = utf-8); $ url = http://www.google.com/ig/api? Weather = shenzhen; // load XML content...