簡體   English   中英

如何從XML獲取元素(標簽)名稱?

[英]How to get the element (tags) name from XML?

  $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:date,dc:maxQuantity。,

如何獲得這種類型的元素?請有人幫我。

您沒有為dc:元素提供任何名稱空間,但它可能與此處描述的內容非常相似

使用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>"; 
  } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM