dongtang1909 2016-07-08 09:14
浏览 27
已采纳

试图让循环中的XML getElementsByTagName + getAttribute调用工作

I have this overly complicated XML dump by Eatec, and sadly each menu has it's own special attribute for meals served at breakfast/lunch/dinner. I'm trying to extract the items with their description and do a loop to show only recipe from breakfast, and so on for other menu periods.

Here's a sample slimmed down XML

<data>
  <menu name="location breakfast" servedate="20160626" location="food court 1" mealperiodname="Breakfast" >
    <recipes>
      <recipe id="5626935" category="HOT MORNING GRAINS" description="Oatmeal RD"> 
      </recipe>
      <recipe id="5371796" category="HOT MORNING GRAINS" description="Rice Brown RD">
      </recipe>   
    </recipes>
  </menu>
  <menu name="location lunch" servedate="20160626" location="food court 2" mealperiodname="Lunch">
     <recipes>
      <recipe id="4430587" category="SOUPS" description="Soup Tomato w/Garden Vegetables">
      </recipe>
      <recipe id="4210899" category="SOUPS" description="Soup Beef Barley w/Vegetables">
      </recipe>
    </recipes>
  </menu>
</data>

And I'm relatively new to PHP/XML, still trying to learn my rope here, here's what I came up with and yet not been able to keep the looped item within it's own meal period.

 <?php

    $menu = new DOMDocument();
    $breakfast = 'Breakfast';
    $lunch = 'Lunch';
    $menu->load("http://amphl.org/test.xml");

    // Get location name
    $menu_get = $menu->getElementsByTagName("menu");
    $location_name = $menu_get->item(0)->getAttribute("location");
    $menu_period = $menu_get->item(0)->getAttribute("name");

    // Get menus
    $recipes = $menu->getElementsByTagName("menu");
    $recipe_items = $menu->getElementsByTagName("recipe");

    // echo tests
    echo '<h3 class="location_date">'.$menu_period.'</h3>';
    echo '<h3 class="location_date">'.$location_name.'</h3>';  

    echo '<div class="meal_items">';
    // echo '<h3 class="food_name"> Breakfast </h3>';
        foreach( $recipes as $recipe )
        {
        // Get recipe name
            $recipe_type = $recipe->getAttribute("mealperiodname");  

            echo '<h3 class="location_date">'.$recipe_type.'</h3>'; 
            if ($recipe_type == $breakfast) {
                foreach( $recipe_items as $recipe_item )
                {
                    $recipe_name = $recipe_item->getAttribute("description"); 
                      echo '<p class="item_list"><a alt="" href="#">'.$recipe_name.'</a></p>';
                }

                }
            else if ($recipe_type == $lunch) {
                foreach( $recipe_items as $recipe_item )
                {
                    $recipe_name = $recipe_item->getAttribute("description"); 
                      echo '<p class="item_list"><a alt="" href="#">'.$recipe_name.'</a></p>';
                }

                }

  }
echo '</div>';

Instead of showing meals for breakfast and lunch in their own loop, it's loading every recipe regarding what meal period is. Am I making it too complicated ? that I got lost by my own code?

  • 写回答

3条回答 默认 最新

  • duanji9677 2016-07-08 10:05
    关注

    I think you tangled yourself

    $menu = new DOMDocument();
    $breakfast = 'Breakfast';
    $lunch = 'Lunch';
    libxml_use_internal_errors(true);
    $menu->load("http://amphl.org/test.xml");
    
    foreach($menu->getElementsByTagName("menu") as $recipes) {
       echo '<h3 class="location_date">'. $recipes->getAttribute('servedate') .'</h3>' ."
    ";
       echo '<h3 class="location_date">'.$recipes->getAttribute('location').'</h3>'  ."
    ";  
       $recipe_type = $recipes->getAttribute("mealperiodname");
       echo '<h3 class="location_date">'.$recipe_type.'</h3>' ."
    "; 
    
       echo '  <div class="meal_items">' . "
    ";
       foreach($recipes->getElementsByTagName("recipe") as $recipe_item) {
          echo '    <p class="item_list"><a alt="" href="#">' .
               $recipe_item->getAttribute("description") .
               '</a></p>'  ."
    ";
       }        
       echo '  </div>' ."
    ";
    }
    

    result

    <h3 class="location_date">20160626</h3>
    <h3 class="location_date">food court 1</h3>
    <h3 class="location_date">Breakfast</h3>
      <div class="meal_items">
        <p class="item_list"><a alt="" href="#">Oatmeal RD</a></p>
        <p class="item_list"><a alt="" href="#">Rice Brown RD</a></p>
      </div>
    <h3 class="location_date">20160626</h3>
    <h3 class="location_date">food court 2</h3>
    <h3 class="location_date">Lunch</h3>
      <div class="meal_items">
        <p class="item_list"><a alt="" href="#">Soup Tomato w/Garden Vegetables</a></p>
        <p class="item_list"><a alt="" href="#">Soup Beef Barley w/Vegetables</a></p>
      </div>
    

    demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!