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

时间:2016-07-08 09:14:13

标签: php xml dom getelementsbytagname getattribute

我有这个过于复杂的Eatec XML转储,遗憾的是每个菜单都有自己的特殊属性,供早餐/午餐/晚餐用餐。我正在尝试用他们的描述提取项目并做一个循环只显示早餐食谱,等等其他菜单期间。

这是一个精简的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>

我对PHP / XML比较陌生,仍然试图在这里学习我的绳索,这就是我想出来的,但却无法将循环项目保留在自己的用餐时间内。

 <?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>';

不是在他们自己的循环中显示早餐和午餐的餐点,而是加载关于用餐时间的每个食谱。我让它变得太复杂了吗?我被自己的代码弄丢了?

3 个答案:

答案 0 :(得分:0)

如果您为变量$recipes = $menu->getElementsByTagName("menu");命名,则相当令人困惑,但我认为您要在foreach( $recipes as $recipe )内使用$mrecipes = $recipe->getElementsByTagName('recipe')来选择内容recipe元素menu元素。

答案 1 :(得分:0)

我认为你纠结了自己

<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>

结果

https://www.facebook.com/dialog/oauth?client_id=<CLIENT_ID>&redirect_uri=http://localhost:8080&response_type=token

<强> demo

答案 2 :(得分:0)

除DOM Api方法外,您还可以使用Xpath获取DOM树的部分内容。 Xpath允许条件,因此您可以获取特定用餐期间的所有菜单节点。

以下是将信息作为文本输出的示例。

$mealPeriods = [
  'Breakfast',
  'Lunch'
];

$document = new DOMDocument();
$document->loadXml($xml);
$xpath = new DOMXpath($document);

// iterate the meal periods
foreach ($mealPeriods as $mealPeriod) {
  echo $mealPeriod, "\n=====================\n";

  // fetch all menu element nodes for a specific meal period
  $expression = "/data/menu[@mealperiodname = '$mealPeriod']";
  foreach ($xpath->evaluate($expression) as $menu) {
    echo "\n", $menu->getAttribute('location'), "\n---------------------\n";

    // iterate the recipe element nodes for a specific menu
    foreach ($xpath->evaluate('recipes/recipe', $menu) as $recipe) {
       echo '#', $recipe->getAttribute('id'), ' ';
       echo $recipe->getAttribute('description'), "\n";
    }
  } 
  echo "\n";  
}

输出:

Breakfast
=====================

food court 1
---------------------
#5626935 Oatmeal RD
#5371796 Rice Brown RD

Lunch
=====================

food court 2
---------------------
#4430587 Soup Tomato w/Garden Vegetables
#4210899 Soup Beef Barley w/Vegetables