2012-01-02 97 views
1

特定節點我有這樣查找從XML文檔

<Person> 
    <firstName>pradeep</firstName> 
    <lastName>jain</lastName> 
    <address> 
    <doorNumber>287</doorNumber> 
    <street>2nd block</street> 
    <city>bangalore</city> 
    </address> 
    <phoneNums type="mobile">9980572765</phoneNums> 
    <phoneNums type="landline">080 42056434</phoneNums> 
    <phoneNums type="skype">123456</phoneNums> 
</Person> 

一個XML我想用PHP來呼應Skype的值。我該怎麼做。我寫了這樣的代碼以下,但使用XPath查詢嘗試它不工作

<?php 
$doc = new DOMDocument(); 

if ($doc->load('new.xml')) 
{ 
    $userInfo = $doc->getElementsByTagName('Person'); 

    foreach($userInfo as $row) 
    { 
     $phoneInfo = $row->getElementsByTagName("phoneNums"); 

     foreach($phoneInfo as $row2) 
     { 
      // get the value from the first child 
      $work = $row2->getElementsByTagName("mobile")->item(0)->nodeValue; 
      $home = $row2->getElementsByTagName("landline")->item(0)->nodeValue; 
      echo $work; 
     } 
    } 
} 
?> 
+0

你會得到什麼錯誤?乍看之下,我無法觀察到任何錯誤。你有沒有嘗試過,例如,在第一個foreach之前,count($ userinfo)的回聲?如果它返回正確的數字,那麼在第二個foreach之前立即返回count($ phoneInfo)的回聲?以便在問題出在哪裏。 – Soph 2012-01-02 19:17:19

+0

@soph - 我得到像嘗試獲取非對象的屬性的錯誤。我正確地計數。我在$ work = $ row2-> getElementsByTagName(「mobile」) - > item(0) - > nodeValue; – Hacker 2012-01-02 19:24:33

+0

好的,如果你嘗試在第二個foreach之前放置一個'echo count($ phoneInfo)',你得到了什麼?? (評論後面的'foreach'部分) – Soph 2012-01-02 19:56:43

回答

3

很抱歉這麼晚纔回復。我剛剛根據您發佈的XML嘗試了這段代碼,但稍有不同,我認爲您的XML包含標籤Person的許多元素。根據this問題建議使用SimpleXML要簡單得多。

<?php 
$xml = simplexml_load_file('path\to\doc.xml'); 
// With the following line you get all the Person tags 
$people = $xml->Person; 
foreach($people as $person) { 
    // For each person you get all the phoneNums tags 
    $phoneNumbers = $person->phoneNums; 
    foreach($phoneNumbers as $key => $value) { 
     $attributes = $value->attributes(); 
     // We get all of the attributes, and select the one on index 0 -the ONLY attribute in this given case 
     if ($attributes[0]=="skype") 
      echo $value; 
    } 
} 
?> 

這適用於像這樣的XML:

<myXml> 
<Person> 
    <firstName>pradeep</firstName> 
    <lastName>jain</lastName> 
    <address> 
    <doorNumber>287</doorNumber> 
    <street>2nd block</street> 
    <city>bangalore</city> 
    </address> 
    <phoneNums type="mobile">9980572765</phoneNums> 
    <phoneNums type="landline">080 42056434</phoneNums> 
    <phoneNums type="skype">123456</phoneNums> 
</Person> 
<Person> 
    <firstName>pradeep</firstName> 
    <lastName>jain</lastName> 
    <address> 
    <doorNumber>287</doorNumber> 
    <street>2nd block</street> 
    <city>bangalore</city> 
    </address> 
    <phoneNums type="mobile">1</phoneNums> 
    <phoneNums type="landline">2</phoneNums> 
    <phoneNums type="skype">3</phoneNums> 
</Person> 
</myXml> 

但是,如果你想與你原來的XML來試試這個(只有一個人的標籤),這個工程:

<?php 
// The following loads the ROOT into $xml (in your case, the Person tag is the root) 
$xml = simplexml_load_file('path\to\doc.xml'); 
// Then we get all its children (firstname, lastname, address, etc) 
$children = $xml->children(); 
// Of all its children, we select the phoneNums tags and then iterate 
$phoneNumbers = $children->phoneNums; 
foreach($phoneNumbers as $key => $value) { 
    $attributes = $value->attributes(); 
    // We get all of the attributes, and select the one on index 0 -the ONLY attribute in this given case 
    if ($attributes[0]=="skype") 
     echo $value; 
} 
?> 
+0

第二個foreach循環裏面沒有打印任何東西。你能再次檢查代碼嗎? – Hacker 2012-01-03 03:49:34

+0

@pradeep我向你展示了我的代碼工作的示例XML。如果您希望它使用您發佈的確切XML,我還添加了代碼。 simpleXML的思想是你可以將XML轉換爲對象,並用普通的屬性選擇器和迭代器處理它的元素(你可以在這裏閱讀更多關於它的內容)(http://php.net/manual/en/book.simplexml .PHP))。 :) – Soph 2012-01-03 13:01:46

+0

,運作良好。但我不能明確地打印Skype號碼 – Hacker 2012-01-03 16:52:52

2

<?php 

$doc = new DOMDocument(); 

if ($doc->load('xpath.xml')) { 

    $xpath = new DOMXPath($doc); 
    $query = '/Person/phoneNums[@type="skype"]'; 
    $results = $xpath->query($query); 

    foreach ($results as $result){ // query may have more than one result 
     echo $result->nodeValue; 
    } 

} 

?> 
+0

- 這是工作 – Hacker 2012-01-03 03:49:50

+0

@Hacker:在哪個意義上? – hakre 2012-08-01 13:42:47