检索xml数据

时间:2011-05-11 01:57:11

标签: php html xml

stores.htm中,我必须输入两个值:一个用于街道地址(streetno和街道名称),另一个用于郊区。在提供输入的基础上,它从stores.xml文件中检索数据。

问题是街道地址和郊区都存储在<address>文件的stores.xml元素中,而不是单独存储。我尝试了很多,但我没有找到如何匹配输入与xml文件数据的解决方案。

我被困在php文件中的if (condition)

stores.xml

<?xml version="1.0" ?>
<stores>
<store>
<name>galaxy</name>
<address>50 Callander Road,Noble park</address>
</store>
<store>
<name>Remica</name>
<address>10 Challander Road,Noble park</address>
</store>
<store>
<name>Home</name>
<address>10/10 Callan Road,Richmond</address>
</store>
</stores>

stores.htm

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head> 
<title>Search</title> 
<script type="text/javascript" src="search.js"></script> 
</head>
<body>
<form> 
Enter Street Address: <input type="text" name="street" /> 
Enter Suburb        : <input type="text" name="suburb" />
<input name="submit" type = "button" onClick = "getData('search.php','info', street.value, suburb.value)" value = "Search"  /> 
</form>

<div id="info" /> 

</body> 
</html> 

的search.php

         $street= $_POST["str"];
         $suburb= $_POST["sub"];

         $xmlFile = "stores.xml";
         $doc= DOMDocument::load($xmlFile);
         $store = $doc->getElementsByTagName("store"); 
         echo "<table border=1><tr><th>Name</th></tr>";   
          foreach($store as $node) 
         {   
        $name = $node->getElementsByTagName("name");
        $name = $name->item(0)->nodeValue;
        $address = $node->getElementsByTagName("address");
        $address = $address->item(0)->nodeValue;     

        if( /* what goes here? */ )  {
            echo"<tr><td>name</td></tr>";   
        }

      echo "</table>";

1 个答案:

答案 0 :(得分:0)

您可以使用PHP的explode function将街道地址与郊区分开。

// split the address into two parts, street and suburb, using delimiter ','
$address_split = explode(',', $address);
$address_street = $address_split[0];
$address_suburb = $address_split[1];

if ($address_street == $street && $address_suburb == $suburb) {
    // code here
}