如何使用php获取XML中所有现有标签的列表 [英] How to get the list of all existing tags in a XML using php

查看:23
本文介绍了如何使用php获取XML中所有现有标签的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的 xml 数据

i have a xml data which looks like this

<channel>
<title>-----</title>
<link>------</link>
<description>---</description>
<lastBuildDate>Tue, 27 Sep 2011 16:37:01 +0000</lastBuildDate>
<language>en</language>
<generator>-------</generator>
<item>
    <title>-----</title>
    <link>-------</link>
    <comments>------</comments>
    <pubDate>Tue, 27 Sep 2011 16:37:01 +0000</pubDate>
    <category>-----</category>
</item>
    <item>
    <title>-----</title>
    <link>-------</link>
    <comments>------</comments>
    <pubDate>Tue, 27 Sep 2011 </pubDate>
    <category>-----</category>
</item>
    <item>
    <title>-----</title>
    <link>-------</link>
    <comments>------</comments>
    <pubDate>Wed, 28 Sep 2011 16:37:01 +0000</pubDate>
    <category>-----</category>
</item>
</channel>

从这里我需要检索所有现有的 XML 标签给用户喜欢,频道,标题,链接,项目等,包括他们的子标签.我的意思是 XML 文件中的所有现有标签我需要关于如何使用 php 来做到这一点的帮助,我使用了 DOM 和简单的 XML 对象,但如果我知道我需要的标签名称,我只能获取特定标签中的值.,

from this i need to retrieve all the existing XML tags to the user like, channel, title, link, item etc., including their child tags too.. i mean all the existing tags in the XML file i need help in how to do that using php, i used DOM and simple XML object but i can only get the values in a specific tag if i know the tag name which i need.,

但我实际上需要处理许多 xml 文件,我不知道该特定 xml 的结构和标签是什么,为此我需要获取现有的标签名称,以便我可以将它们显示给用户选择他需要的标签...

but i actually need to work with many xml files for which i don't know what is the structure and tags of that particular xml, for that i need to get the existing tags names so that i can display them to the user to select what tags he needs...

我需要使用 php 执行此操作的建议.

I need suggestions for doing this using php.,

提前致谢.

推荐答案

这是我的尝试:

$doc = new DOMDocument();

$doc->loadXML( $yourXmlString ); // or:
$doc->load( $yourXmlUrl );

$xpath = new DOMXpath( $doc );
$nodes = $xpath->query( '//*' );

$nodeNames = array();
foreach( $nodes as $node )
{
    $nodeNames[ $node->nodeName ] = $node->nodeName;
}

var_dump( $nodeNames );

我希望我可以让 xpath 表达式更高效一点,但我想不出任何东西.这就是为什么我不断覆盖 $nodeNames 的键.

I wish I could have made the xpath expression a little more efficient though, but I can't think of anything. That's why I continuously overwrite the key of $nodeNames.

想想看:也许我误解了您的问题,您根本不想要唯一的元素名称,而是想要字面上的所有元素.如果是这样:你想要他们怎么样?作为字符串?包括他们的完整路径?

Come to think of it: perhaps I've misunderstood your question and you don't want the unique element names at all, but want literally all elements. If so: how do you want them? As strings? Including their full path?

这篇关于如何使用php获取XML中所有现有标签的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆