http://www.meituan.com/api/v2/beijing/deals
如何使用  DOM 或者 SimpleXML  获得这个页面上的所有标签名   和标签值,按照  http://www.w3school.com.cn/php/php_xml_simplexml.asp  这里的教程只能获得固定的  .xml  文件里的标签名,至于远程返回的xml文件不知道如何获得标签名请教各位帮忙解决下。

解决方案 »

  1.   

    $xml=file_get_contents('http://www.meituan.com/api/v2/beijing/deals');$obj=SimpleXMLElement($xml);......
      

  2.   

    晕~ 是 $obj=new SimpleXMLElement($xml);
      

  3.   

    <?php
     function xml2assoc($xml) {
     $assoc = null;
     $attr = null;
     $tmpName = null;
     
     while($xml->read()){
         switch ($xml->nodeType) {
          case XMLReader::END_ELEMENT: return $assoc;
          case XMLReader::ELEMENT:
             $assoc[$xml->name] =  $xml->isEmptyElement ? '' : xml2assoc($xml);
            echo "<pre></pre>";
       
    //print_r($xml->name);
          //  print_r($assoc[$xml->name]); 
             //如果此节点下还有子节点,
             //将递归调用xml2assoc,直到解析到本节点的最深节点
             $tmpName = $xml->name;
             
        //     echo "<pre></pre>";
         //    print_r($tmpName);
             
             //这里$xml->name指节点名称:
             //<chester>good</chester>
             //其中chester为$xml->name
             //good为$xml-value;
             
      //       print_r($xml->hasAttributes);
             if($xml->hasAttributes){
             
            // print_r($xml->moveToNextAttribute());
                 while($xml->moveToNextAttribute()){

    //  echo "<pre></pre>";
            //     print_r($xml->value);
             //    print_r($xml->name);
                 
                      $tmpArr[$xml->name] = $xml->value;
                      //这里$xml->name为属性名称
                      //<chester id=0></chester>
                      //id为$xml->name,0为$xml->value;
                 }
                 
    //  echo "<pre></pre>";
            //     print_r($tmpArr);             
                
                 $assoc[$tmpName]['attr'] = $tmpArr;
          //      echo '$assoc[$tmpName][attr]'; 
            //    print_r($assoc[$tmpName]['attr']); 
             //    print_r($assoc[$tmpName]['attr']);  
                 unset($tmpArr);//清除到临时属性数组,不然属性值将一直叠加到上次数组中
              }
             break;
           case XMLReader::TEXT:
           case XMLReader::CDATA:
                $assoc['value'] .= $xml->value;
         }
       }
       return $assoc;
     }
     
       
     
     $xml = new XMLReader();
     $xml->open('http://interface.hd.sohu.com/360/movies.xml');
     $assoc = xml2assoc($xml);
     echo "<br></br>"; 
    echo $title=$assoc['document']['item']['title']['value'];echo $webname=$assoc['document']['webName']['value'];
     $xml->close();
     echo '<pre>';
     print_r($assoc);
     echo '</pre>';
     
     ?>
      

  4.   

    $obj->getName(); 获取标签名称
      

  5.   

    恩试了,没反应
    代码如下
    <?php
    $xml = file_get_contents('http://www.meituan.com/api/v2/beijing/deals');
    $obj=new SimpleXMLElement($xml);echo $obj->getName() . "<br />";foreach($obj->children() as $child)
      {
      echo $child->getName() . ": " . $child . "<br />";
      }?>
      

  6.   


    $xml=file_get_contents('http://www.meituan.com/api/v2/beijing/deals');
    $obj=new simpleXMLElement($xml);
    //deal 
    foreach($obj->deals->data->deal->children() as $child){
    echo $child->getName()."\r";
    };
    //shop
    foreach($obj->deals->data->shops->shop->children() as $child){
    echo $child->getName()."\r";
    }你要根据object的结构来取节点啊