如何从XML获取元素(标签)名称?

时间:2012-03-01 05:39:45

标签: php xml cakephp

  $xml = simplexml_load_file($URL);
            foreach($xml->children() as $child)
            {
                 foreach($child as $child)
                 {
                     $list[] = $child->getName();
                 }
            } 

<channel>
        <title></title>
        <link></link>
        <description></description>
        <item>
            <title></title>
            <link></link>
            <description></description>
            <pubDate></pubDate>
            <guid isPermaLink="false"></guid>
            <dc:date></dc:date>
            <dc:maxQuantity></dc:maxQuantity>
        </item>     
    <channel> 
我得到了这样的答案 标题, 链接, 描述, pubdate的, guid

我没有得到

DC:日期, DC:maxQuantity,

如何获得这类元素?请任何人帮助我。

2 个答案:

答案 0 :(得分:0)

您没有为dc:元素提供任何名称空间,但它可能非常类似于 what is described here

答案 1 :(得分:0)

使用php的DOMDocument。

$objDOM = new DOMDocument(); 
  $objDOM->load("test.xml"); //make sure path is correct 


  $note = $objDOM->getElementsByTagName("lon"); 
  // for each note tag, parse the document and get values for 
  // tasks and details tag. 

  foreach( $note as $value ) 
  { 
    $tasks = $value->getElementsByTagName("id"); 
    $task  = $tasks->item(0)->nodeValue; 


    $details = $value->getElementsByTagName("name"); 
    $detail  = $details->item(0)->nodeValue; 

    $notes = $value->getElementsByTagName("notes"); 
    $notes  = $notes->item(0)->nodeValue; 

    echo "$task :: $detail :: $notes <br>"; 
  }