SimpleXML-如何列出随机子列表的属性



我有一些xml,它有多个子级。它们的名字是已知的,数量和顺序是未知的。在这种情况下,它们可以是FreeRide或SteadyState。

我需要保留它们的顺序,然后提取持续时间和力量的属性。

<?php
$xml='<workout_file>
<workout>
<FreeRide Duration="300" Power="1.3"/>
<SteadyState Duration="300" Power="0.55"/>
<FreeRide Duration="10"  Power="1.2"/>
<SteadyState Duration="180" Power="0.55"/>
<FreeRide Duration="10"  Power="1.1"/>
<SteadyState Duration="120" Power="0.55"/>
</workout>
</workout_file>';
$xml=simplexml_load_string($xml);
$count = 0;
foreach($xml->workout->children() as $node ){
$type = $node->getName();
echo $type." position: ".$count."<br>";
$count++;
//Get Attributes
//$attr =  $xml->workout->$$type[$count]->attributes(); //Doesn't work Undefined variable: FreeRide
//echo "Duration: ".$attr['Duration']."<br>";
//echo "Power: ".$attr['Power']."<br>";
}

我可以用这个代码列出孩子们,以及他们的位置。然而,我不确定如何获得属性,因为确切的子名称是未知的(它们可以按任何顺序(。

注意:此列表仅列出FreeRide儿童:$attr=$xml->锻炼->FreeRide[$count]->attributes((;

如何按正确顺序获得子项、持续时间和功率的列表?

这是使用DOM而不是SimpleXML的一个很好的例子。根据我的经验,在除最基本的情况外的所有情况下,SimpleXML通常比DOM更难使用。

<?php
$xml='<workout_file>
<workout>
<FreeRide Duration="300" Power="1.3"/>
<SteadyState Duration="300" Power="0.55"/>
<FreeRide Duration="10"  Power="1.2"/>
<SteadyState Duration="180" Power="0.55"/>
<FreeRide Duration="10"  Power="1.1"/>
<SteadyState Duration="120" Power="0.55"/>
</workout>
</workout_file>';
//Instantiate a DOMDocument and load our XML
$doc = new DOMDocument();
$doc->loadXML($xml);
// Instantiate DOMXpath with our document
$xpath = new DOMXPath($doc);
//Get all of our workout elements and iterate through them
$workoutElements = $xpath->query('/workout_file/workout');
foreach($workoutElements as $currElement)
{
//Iterate through the children
$count = 0;
foreach($currElement->childNodes as $currIntervalNode)
{
/*
* Since we're going through ALL of the children, some of them will be whitespace nodes we don't care about,
* so only look at nodes with attributes
*/
if($currIntervalNode->hasAttributes())
{
// the nodeName attribute is the tag name
$type = $currIntervalNode->nodeName;
// Pull out the attribute vaules by name
$duration = $currIntervalNode->getAttribute('Duration');
$power = $currIntervalNode->getAttribute('Power');
echo 'Position: '.$count++.'<br>'.PHP_EOL;
echo 'Type: '.$type.'<br>'.PHP_EOL;
echo 'Duration: '.$duration.'<br>'.PHP_EOL;
echo 'Power: '.$power.'<br>'.PHP_EOL;
}
}
}

我觉得你想得太多了。您有一个表示XML节点的变量,并且需要该节点的属性。就这么简单:

//Get Attributes
echo "Duration: ".$node['Duration']."<br>";
echo "Power: ".$node['Power']."<br>";

(如果您正在处理名称空间,或者如果您想在节点的所有属性上循环,则只需要显式调用->attributes(),这两种情况在本例中都不适用。(

所以总计:

<?php
$xml='<workout_file>
<workout>
<FreeRide Duration="300" Power="1.3"/>
<SteadyState Duration="300" Power="0.55"/>
<FreeRide Duration="10"  Power="1.2"/>
<SteadyState Duration="180" Power="0.55"/>
<FreeRide Duration="10"  Power="1.1"/>
<SteadyState Duration="120" Power="0.55"/>
</workout>
</workout_file>';
$xml=simplexml_load_string($xml);
$count = 0;
foreach($xml->workout->children() as $node ){
$type = $node->getName();
echo $type." position: ".$count."<br>";
$count++;
//Get Attributes
echo "Duration: ".$node['Duration']."<br>";
echo "Power: ".$node['Power']."<br>";
}

现场演示

最新更新