递归显示嵌套的XML和属性PHP

时间:2019-02-01 18:03:48

标签: php xml recursion

此操作的最终目标是加载XML设置文件并能够通过html表单对其进行修改。我什至无法加载带有其属性的嵌套XML来打印,这是我的第一步。

这是XML的摘要:

<Settings>
    <GSettings>
        <Name Value="Plane"/>
        <Id Value="50"/>
        <Timeout Value="100"/>
    </GeneralSettings>
    <WingSetting>
        <LeftWing Color="Red" Value="1"/>
        <RightWing Value="0"/>
        <MiddleBar>
            <Fields>
                <Value>Direction</Value>
                <Value>Speed</Value>
                <Value>Height</Value>
            </Fields>
        </MiddleBar>
    </WingSetting>

因此,类似这样的目标是按照以下内容打印内容:

.GSettings.Name = 
ATTRIBUTES:
Value = Plane

.GeneralSettings.Id = 
ATTRIBUTES:
Value = 50

.GeneralSettings.Timeout = 
ATTRIBUTES:
Value = 100

.WingSetting.LeftWing =
ATTRIBUTES:
Value = 1
Color = Red

.WingSetting.RightWing =
ATTRIBUTES:
Value = 0


.WingSetting.MiddleBar=


.WingSetting.MiddleBar.Fields.Value=Direction

.WingSetting.MiddleBar.Fields.Value=Speed

.WingSetting.MiddleBar.Fields.Value=Height

我能够递归地打印XML和子级,并且在中间栏字段的情况下,我可以打印速度方向和高度。但是我不知道该如何获取属性并随即打印它们。当前代码是这样的:

$xml=simplexml_load_file("MySettings.xml");
RecurseXML($xml);

function RecurseXML($xml,$parent="")
{
   $child_count = 0;
   foreach($xml as $key=>$value)
   {

      $child_count++;    
      if(RecurseXML($value,$parent.".".$key) == 0)  
      {

         echo($parent . "." . (string)$key . " = " . (string)$value . "<BR>\n");       
      }    
   }
   return $child_count;
} 

除了没有属性选项外,它完全按照我的要求打印。我知道有一个attribute()选项,但我不知道如何添加它,因此它会递归地执行。关于如何添加它的任何想法?

1 个答案:

答案 0 :(得分:1)

在回显行之后添加此代码

Error in .local(.Object, ...) :
An error occurred while creating a virtual connection to the DAP server:
Error while reading the URL: https://disc2.gesdisc.eosdis.nasa.gov:443/openda
p/ncml/aggregation/TRMM_3B42_Daily.7/TRMM_3B42_daily.7_Aggregation_2001.ncml.
ver.
The OPeNDAP server returned the following message:
Unauthorized: Contact the server administrator.
Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer",
Cannot create a RasterLayer object from this file. (file does not exist)