2014-11-05 58 views
2

所以,當我使用PHP與MySQL我只是做:從XML文件中選擇所有的人在哪裏=「鮑勃」

$result = $mysqli->query("SELECT * FROM table WHERE person='Bob'"); 

我現在使用PHP的XML和我沒有帶一個線索如何做這個。我試過Google搜索,但我不知道這將在XML中被調用。

我的XML是:

<People> 
    <person>Bob</person> 
    <about>Bob is a blob. He is 20 Years old, etc</about> 
</People> 
<People> 
    <person>Jim</person> 
    <about>Jim is 90 Years old, he plays basketball, etc</about> 
</People> 
<People> 
    <person>Steve</person> 
    <about>Steve is a superhero with the ability to fly.</about> 
</People> 

基本上,我想回應某個人的信息。因此,如果吉姆用戶搜索,將呼應:

Name: Jim 
About: Jim is 90 Years old, he plays basketball, etc 
+2

http://en.wikipedia.org/wiki/XPath – 2014-11-05 21:41:37

+1

請,[唐」使用'mysql_ *'函數](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php),它們不再被維護並且是[offic最後棄用](https://wiki.php.net/rfc/mysql_deprecation)。學習[準備的語句](http://en.wikipedia.org/wiki/Prepared_statement),並使用[PDO](http://us1.php.net/pdo)或[MySQLi](http:// us1.php.net/mysqli)。 [本文](http://php.net/manual/en/mysqlinfo.api.choosing.php)將幫助你決定。 – 2014-11-05 21:47:07

回答

2

使用XPath

XML:

<data> 
    <People> 
     <person>Bob</person> 
     <about>Bob is a blob. He is 20 Years old, etc</about> 
    </People> 
    <People> 
     <person>Jim</person> 
     <about>Jim is 90 Years old, he plays basketball, etc</about> 
    </People> 
    <People> 
     <person>Steve</person> 
     <about>Steve is a superhero with the ability to fly.</about> 
    </People> 
</data> 

PHP代碼:

$xml = new SimpleXMLElement($xml_string); 
$result = $xml->xpath('/data/People[person="Jim"]'); 
var_dump($result); 

輸出:

array(1) { 
    [0]=> 
     object(SimpleXMLElement)#2 (2) { 
      ["person"]=> 
       string(3) "Jim" 
      ["about"]=> 
       string(45) "Jim is 90 Years old, he plays basketball, etc" 
     } 
} 
+0

你會如何用CSS來清理它,並且清理一下它,以便它說: '姓名:Jim 關於:吉姆90歲,他打籃球等# – one2three 2014-11-05 22:38:18

+0

就像你用MySQL的結果一樣 – AlexL 2014-11-05 22:40:32

+0

使用MySQL我使用了Div。那麼,我是否也會這樣做XML – one2three 2014-11-05 22:59:39