Tuesday, June 2, 2009

Search Records into XML Using PHP

There is an example for search items or block into xml using php with simplexml_load_string method or function, this will return you record block for particular query.

query = us

CODE:
1 :  <?php
2 :       # parm query=delhi , us
3 :       $xmlStr = '<?xml version="1.0" encoding="utf-8"?>
4 :                           <temprature>
5 :                           <item>
6 :                                <city>Bareilly</city>
7 :                                <state>Uttar Pradesh</state>
8 :                                <country>India</country>
9 :                                <lat>32.4567</lat>
10 :                                <lon>34.4567</lon>
11 :                                <temp>43.0</temp>
12 :                           </item>
13 :                           <item>
14 :                                <city>Haldwani</city>
15 :                                <state>Uttarakhand</state>
16 :                                <country>India</country>
17 :                                <lat>33.4567</lat>
18 :                                <lon>34.4567</lon>
19 :                                <temp>44.0</temp>
20 :                           </item>
21 :                           <item>
22 :                                <city>Delhi</city>
23 :                                <state>Delhi</state>
24 :                                <country>India</country>
25 :                                <lat>35.4567</lat>
26 :                                <lon>36.4567</lon>
27 :                                <temp>45.0</temp>
28 :                           </item>
29 :                           <item>
30 :                                <city>Jaipur</city>
31 :                                <state>Rajasthan</state>
32 :                                <country>India</country>
33 :                                <lat>36.4567</lat>
34 :                                <lon>37.4567</lon>
35 :                                <temp>48.0</temp>
36 :                           </item>
37 :                           </temprature>';
38 :       $xmlo = simplexml_load_string($xmlStr);
39 :       $i=0;
40 :       foreach($xmlo->item as $c){
41 :            foreach($c->children() as $ch){
42 :                 if(strtolower($ch) == strtolower($_GET["query"])){
43 :                      @header("content-type: text/xml");
44 :                      echo $xmlo->item[$i]->asXML();
45 :                      exit;
46 :                 }
47 :            }
48 :            $i++;
49 :       }
50 :       @header("content-type: text/xml");
51 :       echo "<Error>No item found!</Error>";
52 :       exit;
53 :  ?>

No comments:

Post a Comment