php-使用XML进行解析,但是元素名称是动态的
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php-使用XML进行解析,但是元素名称是动态的,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3561字,纯文字阅读大概需要6分钟。
内容图文
![php-使用XML进行解析,但是元素名称是动态的](/upload/InfoBanner/zyjiaocheng/695/b46056ea8d9d4b9fb4874aee44fa6ee5.jpg)
Simple XMLElement Object
(
[IpStatus] => 1
[ti_pid_20642] => SimpleXmlElement Object
(
我有上述格式的SimpleXMLElment,并且此XML在运行时生成,并且它的节点值(例如ti_pid_20642)部分为dnymaic,例如ti_pid_3232,ti-pid_2323,ti_pid_anyumber.
我的问题是如何获取这些节点的值以及使用PHP的子节点?
解决方法:
要获取在SimpleXML的XML字符串中使用的所有节点名称,可以使用SimpleXMLIterator:
$tagnames = array_keys(iterator_to_array(
new RecursiveIteratorIterator(
new SimpleXMLIterator($string)
, RecursiveIteratorIterator::SELF_FIRST
)
));
print_r($tagnames);
这可以给您示例(您在问题中未提供任何XML,Demo):
Array
(
[0] => IpStatus
[1] => ti_pid_20642
[2] => dependend
[3] => ti-pid_2323
[4] => ti_pid_anyumber
[5] => more
)
如果您在提供包含有效XML的字符串时遇到问题,请使用现有的SimpleXMLelement和create an XML string out of it:
$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):
$it = new RecursiveIteratorIterator(
new SimpleXMLElementIterator($xml), RecursiveIteratorIterator::SELF_FIRST
);
$tagnames = array_keys(iterator_to_array($it));
这仅取决于您的需求.
使用命名空间元素,这变得不那么直接.取决于您是否只想获取本地名称,还是要获取带有标记名的命名空间URI或命名空间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);
}
输出(Demo):
#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
玩得开心.
内容总结
以上是互联网集市为您收集整理的php-使用XML进行解析,但是元素名称是动态的全部内容,希望文章能够帮你解决php-使用XML进行解析,但是元素名称是动态的所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。