<Property>
<Name>TaskId</Name>
<Value>12345</Value>
</Property>
<Property>
<Name>TaskName</Name>
<Value>Task1</Value>
</Property>
<Property>
<Name>TaskId</Name>
<Value>6789</Value>
</Property>
<Property>
<Name>TaskName</Name>
<Value>Task2</Value>
</Property>
有谁可以帮助我如何检索所有taskID?
期望的输出: 12345 6789
提前致谢。
答案 0 :(得分:0)
这是一个有效的xml吗?我认为不是
但无论如何:
<?php
$test = <<<EOT
<root>
<Property>
<Name>TaskId</Name>
<Value>12345</Value>
</Property>
<Property>
<Name>TaskName</Name>
<Value>Task1</Value>
</Property>
<Property>
<Name>TaskId</Name>
<Value>6789</Value>
</Property>
<Property>
<Name>TaskName</Name>
<Value>Task2</Value>
</Property>
</root>
EOT;
$document = @DOMDocument::loadXML($test);
$xpath = new DOMXpath($document);
$task_id_elements = $xpath->query('//Property//Name[text()="TaskId"]/following-sibling::*');
$task_ids = array();
foreach ($task_id_elements as $task_id_element) {
$task_ids[] = $task_id_element->nodeValue;
}
echo implode(" ",$task_ids);
答案 1 :(得分:0)
$objectList = $doc->getElementsByTagName('Object');
for ($o=0;$o<$objectList->length;$o++)
{
$objectNode = $objectList->item($o);
$properyList = $objectNode->getElementsByTagName('Property');
for ($p=0;$p<$properyList->length;$p++)
{
$propertyNode = $properyList->item($p);
$name = $propertyNode->getElementsByTagName('Name')->item(0)->nodeValue;
$value = $propertyNode->getElementsByTagName('Value')->item(0)->nodeValue;
$d[$name] = $value;
}
$task_data[] = $d;
unset($d);
}
foreach ($task_data as $td)
{
echo $td["TaskId"].": ".$td["State"]."<br/>";
}
另一种方式,这是有效的! :)