获取 XML 中使用的所有节点名称string通过 SimpleXML,您可以使用SimpleXMLIterator
:
$tagnames = array_keys(iterator_to_array(
new RecursiveIteratorIterator(
new SimpleXMLIterator($string)
, RecursiveIteratorIterator::SELF_FIRST
)
));
print_r($tagnames);
这可以给你示范(你没有在你的问题中给出任何XML,Demo http://codepad.org/QWhqD3sh):
Array
(
[0] => IpStatus
[1] => ti_pid_20642
[2] => dependend
[3] => ti-pid_2323
[4] => ti_pid_anyumber
[5] => more
)
如果您在提供包含有效 XML 的字符串时遇到问题,请使用现有的 SimpleXMLelement 并用它创建一个 XML 字符串 http://php.net/simplexmlelement.asxml:
$string = $simpleXML->asXML();
但是,如果您想从 SimpleXML 对象获取所有标记名,但不想将其转换为字符串,则可以为以下内容创建递归迭代器:SimpleXMLElement
还有:
class SimpleXMLElementIterator extends IteratorIterator implements RecursiveIterator
{
private $element;
public function __construct(SimpleXMLElement $element) {
parent::__construct($element);
}
public function hasChildren() {
return (bool)$this->current()->children();
}
public function getChildren() {
return new self($this->current()->children());
}
}
它的用法类似(Demo http://codepad.org/po8qVodV):
$it = new RecursiveIteratorIterator(
new SimpleXMLElementIterator($xml), RecursiveIteratorIterator::SELF_FIRST
);
$tagnames = array_keys(iterator_to_array($it));
这仅取决于您需要什么。
对于命名空间元素,这变得不那么直接。取决于您是否只想获取本地名称或命名空间名称,甚至是带有标签名称的命名空间 URI。
给定的SimpleXMLElementIterator
可以更改为支持跨命名空间的元素迭代,默认情况下 simplexml 仅提供默认命名空间中元素的遍历:
/**
* SimpleXMLElementIterator over all child elements across namespaces
*/
class SimpleXMLElementIterator extends IteratorIterator implements RecursiveIterator
{
private $element;
public function __construct(SimpleXMLElement $element) {
parent::__construct(new ArrayIterator($element->xpath('./*')));
}
public function key() {
return $this->current()->getName();
}
public function hasChildren() {
return (bool)$this->current()->xpath('./*');
}
public function getChildren() {
return new self($this->current());
}
}
然后,您需要检查每个元素的命名空间 - 作为使用命名空间的修改后的 XML 文档的示例:
<root xmlns="namspace:default" xmlns:ns1="namespace.numbered.1">
<ns1:IpStatus>1</ns1:IpStatus>
<ti_pid_20642>
<dependend xmlns="namspace:depending">
<ti-pid_2323>ti-pid_2323</ti-pid_2323>
<ti_pid_anyumber>ti_pid_anyumber</ti_pid_anyumber>
<more xmlns:ns2="namspace.numbered.2">
<ti_pid_20642 ns2:attribute="test">ti_pid_20642</ti_pid_20642>
<ns2:ti_pid_20642>ti_pid_20642</ns2:ti_pid_20642>
</more>
</dependend>
</ti_pid_20642>
</root>
结合更新SimpleXMLIterator
下面的示例代码演示了新行为:
$xml = new SimpleXMLElement($string);
$it = new RecursiveIteratorIterator(
new SimpleXMLElementIterator($xml), RecursiveIteratorIterator::SELF_FIRST
);
$count = 0;
foreach ($it as $name => $element) {
$nsList = $element->getNamespaces();
list($ns, $nsUri) = each($nsList);
printf("#%d: %' -20s %' -4s %s\n", ++$count, $name, $ns, $nsUri);
}
Output (Demo http://codepad.org/zc7rrgz6):
#1: IpStatus ns1 namespace.numbered.1
#2: ti_pid_20642 namspace:default
#3: dependend namspace:depending
#4: ti-pid_2323 namspace:depending
#5: ti_pid_anyumber namspace:depending
#6: more namspace:depending
#7: ti_pid_20642 namspace:depending
#8: ti_pid_20642 ns2 namspace.numbered.2
玩得开心。