简体   繁体   中英

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> 

i got the answer like this title, link, description, pubDate, guid

i'm not getting the

dc:date, dc:maxQuantity.,

how to get the this type of elements?please any one help me.

您没有为dc:元素提供任何名称空间,但它可能与此处描述的内容非常相似

Use DOMDocument of php.

$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>"; 
  } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM