php
  • xml
  • dom
  • domdocument
  • 2011-09-25 43 views 0 likes 
    0

    我如何讀取在PHP中第一個也是唯一的評論:我如何閱讀PHP中的第一個XML註釋?

    <?xml version="1.0" encoding="UTF-8"?> 
    <!-- read this --> 
    <root> 
    </root> 
    

    使用DOMDocument對象?

    $xml = new DOMDocument(); 
    
    $libxml_error = 'Il file %s non risulta ben formato (%s).'; 
    
    // Per evitare i warning nel caso di xml non ben formato occorre sopprimere 
    // gli errori 
    if ([email protected]$xml -> load($xml_file_path)) $error = sprintf($libxml_error, 
        $xml_file_path, libxml_get_last_error()->message); 
    
    // Read the fist comment in xml file 
    

    回答

    4

    這個怎麼樣:

    $xpath = new DOMXPath($xml); 
    foreach ($xpath->query('//comment()') as $comment) { 
        var_dump($comment->textContent); 
    } 
    

    因爲你很可能只想要第一個:

    $xpath = new DOMXPath($xml); 
    $results = $xpath->query('//comment()'); 
    $comment = $results[0]; 
    var_dump($comment); 
    

    來源How to retrieve comments from within an XML Document in PHP

    +0

    好。爲什麼不/ /評論()[1]? – gremo

    +0

    由於'DOMXPath :: query'總是返回一個節點列表。基本上,即使你要查詢'... [1]',你仍然得到一個類似數組的對象(所以你仍然需要添加額外的'$ comments = $ results [0];'行)。 **來源**:http://www.php.net/manual/en/domxpath.query.php – elslooo

    0

    如何從XML的意見文件 - 在這裏尋找說明:

    How to retrieve comments from within an XML Document in PHP

    相關問題