2012-03-06 195 views
0

請幫助我需要通過屬性 例如獲得價值PHP和屬性的XML獲取價值

$xml = simplexml_load_file($filename); 
print $xml->attribute->name('header'); 

輸出: HEADER只有

和XML文件

<template name="header" type="tpl"> 
**HEADER** 
</template> 
<template name="body" type="css"> 
BODY 
</template> 
<template name="footer" type="tpl"> 
FOOTER 
</template> 

回答

0

您給出的XML作爲示例引發了各種分析錯誤。現在,我假設你有一個像,下面一個可行的,有效的XML:

<?xml version="1.0"?> 
<templates> 
    <template name="header" type="tpl"> 
    **HEADER** 
    </template> 
    <template name="body" type="css"> 
    BODY 
    </template> 
    <template name="footer" type="tpl"> 
    FOOTER 
    </template> 
</templates> 

就這樣,在訪問名爲header模板可以用這種方式來完成:

<?php 
    $filename = "xmlparse01.xml"; 
    $xml = simplexml_load_file($filename); 
    $reslt = $xml->xpath("//template[@name='header']"); 
    print trim($reslt[0]) . "\n"; 
?> 
+0

是的非常感謝你 – user1179177 2012-03-06 21:46:31

+0

很高興能有所幫助。如果你喜歡,也許你可以通過成爲會員並接受我的答案來表明這解決了你的問題。我想你可能會發現它值得使用StackOverflow :)。 – 2012-03-06 21:49:49

0

我不太瞭解你的問題,但是如果你想知道如何訪問XML e的header屬性您可以這樣做:

$xml = simplexml_load_file($filename); 
print $xml['header']; 
+0

沒有,我需要得到模板** HEADER ** – user1179177 2012-03-06 20:53:35