2014-11-04 39 views
1

我有xml文件$file.xml包括以下如何檢查XML文件是否已使用PHP

<?xml version="1.0"?> 
<names><name>name1</name><name>name2</name><name>name3</name></names> 

我需要一種方法來檢查,如果<names>有不止一個孩子<name>這意味着,如果一個以上的孩子值該文件具有超過<name>name1</name> whick意味着如果<name>name2</name>或更多存在調用函數。

我曾嘗試以下

$file = "file.xml"; 

    $result = file_get_contents($file); 

    if(@count(file($result)) > 1) { 

    //perform a function 
    } 

file_get_contents已經給我的價值觀name1name2name3

if(@count(file($result)) > 1) {什麼也沒做事先

感謝

回答

2

是其相當可能的話,你可以在conjun中使用xpath ction這種情況:

$file = "file.xml"; 
$xml = simplexml_load_file($file); 
$names = $xml->xpath('/names/name'); 
if(count($names) > 1) { 
    //perform a function 
    echo 'perform!'; 
} 

或用DOMDocument替代使用XPath和:

$dom = new DOMDocument(); 
$dom->load($file); 

$count = $xpath->evaluate('count(/names/name)'); 
if($count > 1) { 
    echo 'perform!'; 
} 
+0

非常感謝,我使用選項1,除非你認爲選項2更好 對不起,我不能投票: ) – guestofhonor77 2014-11-04 14:30:19

+0

@ guestofhonor77肯定沒有問題,他們都很好。我很高興這有幫助 – Ghost 2014-11-04 15:00:19