PHP和XML。使用PHP循环访问XML文件 [英] PHP and XML. Looping through an XML file with PHP

查看:148
本文介绍了PHP和XML。使用PHP循环访问XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正深深地尝试着使用PHP来处理这个XML文件(下面是实际的XML文本)(下面的XML文件内容)。
我正在尝试做什么如下:


  1. 获取所有文件夹元素名称

  2. 如果文件夹元素具有是子文件夹属性,然后向下移动一级并获取该文件夹元素的名称

  3. 如果不移动到下一个文件夹元素

gallerylist.xml:

 <?xml version =1.0encoding =ISO-8859 -1\" >?; 
< gallerylisting exists =yes>
< folder subfolder =yes>
活动
< folder子文件夹=是>
Beach_Clean_2010
< folder subfolder =no>
Onna_Village
< /文件夹>
< folder subfolder =no>
Sunabe_Sea_Wall
< / folder>
< /文件夹>
< /文件夹>
< folder subfolder =no>
Food_And_Drink
< / folder>
< folder subfolder =no>
里面
< /文件夹>
< folder subfolder =no>
位置
< /文件夹>
< folder subfolder =no>
夜生活
< /文件夹>
< / gallerylisting>

gallerylisting.php

 <?php 
$ xmlref = simplexml_load_file(gallerylisting.xml);
foreach($ xmlref-> children()as $ child){
foreach($ child-> attributes()as $ attr => $ attrVal){
print $ child ;
if($ attrVal ==yes){
foreach($ child-> children()as $ child){
echo $ child;
foreach($ child-> attributes()as $ attr => $ attrVal){
if($ attrVal ==yes){
foreach($ child-> children()as $ child){
echo $ child;
}
}
}
}
}
}
}

我是...计数... 5 foreach循环深入这个PHP脚本,我不喜欢它,再加上如果我的文件夹有另一个子文件夹,我会不得不添加同样的

  $ if(attrVal ==yes)...等等。 

再次以及...不!无论如何,我可以避免这一点。我是PHP的新手,尤其是PHP和XML。

感谢您的帮助。

解决

 <?php 
$ b方案

递归可能对您有益。 $ b函数display_entities($ xml)
{
foreach($ xml-> children()as $ child){
foreach($ child-> attributes()as $ attr = > $ attrVal){
print $ child;
if($ attrVal ==yes){
display_entities($ child-> children());




$ xmlref = simplexml_load_file(gallerylisting.xml);

display_entities($ xmlref-> children());


I am knee deep in foreach purgatory right now trying to come up with a way to traverse this XML file (actual XML text below) with PHP(following the XML file content.) What I am trying to do is the following:

  1. Get all folder element names
  2. If the folder element has yes as a subfolder attribute, then move a level down and grab that folder element's name
  3. If not move on to the next folder element

gallerylist.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<gallerylisting exists="yes">
<folder subfolder="yes">
Events
   <folder subfolder="yes">
   Beach_Clean_2010
        <folder subfolder="no">
        Onna_Village
        </folder>
            <folder subfolder="no">
            Sunabe_Sea_Wall
        </folder>
        </folder>
  </folder>
  <folder subfolder="no">
  Food_And_Drink
  </folder>
  <folder subfolder="no">
  Inside
  </folder>
  <folder subfolder="no">
  Location
  </folder>
  <folder subfolder="no">
  NightLife
  </folder>
</gallerylisting>

gallerylisting.php

<?php
$xmlref = simplexml_load_file("gallerylisting.xml");
foreach($xmlref->children() as $child) {
    foreach($child->attributes() as $attr => $attrVal) {
        print $child;
        if($attrVal == "yes") {
            foreach($child->children() as $child) {
                echo $child;
                foreach($child->attributes() as $attr => $attrVal) {
                    if($attrVal == "yes") {
                        foreach($child->children() as $child) {
                            echo $child;
                        }
                    }                   
                }
            }
        }
    }
}

I am...counting...5 foreach loops deep into this PHP script and I do not like it at all, plus if my folders had another subfolder, I would have to add this same

$if(attrVal=="yes")...etc.

in again and well...no! Is there anyway at all that I can avoid this. I'm new to PHP, and especially PHP and XML.

Thanks for any help.

解决方案

Recursion could be beneficial to you here.

<?php

function display_entities( $xml )
{
    foreach($xml->children() as $child) {
        foreach($child->attributes() as $attr => $attrVal) {
            print $child;
            if($attrVal == "yes") {
              display_entities( $child->children() );
            }
        }
    }
}

$xmlref = simplexml_load_file("gallerylisting.xml");

display_entities($xmlref->children());

这篇关于PHP和XML。使用PHP循环访问XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆