如何读取我不知道的随机/动态 xml 标签名称?
我想执行如下 xml 数据交换:
传入的 xml 数据有一些我不知道标签名称的标签。
我可以在不知道标签名称的情况下读取这些标签吗? 另外,如何获取标签名称作为数据属性?
例如: 我想阅读不同的患者记录。每个患者都有不同的疾病。例如“心脏病”、“癌症”。
<heart disease>serious</heart disease>
<cancer>normal</cancer>
我以前不知道这两个标签。但我想读取标签名称并呈现它。 最后,我可以获取数据: 心脏病:严重 癌症正常:
I would like to perform a xml data exchange as below:
The incoming xml data have some tags i don't know about the tag name.
Can i read those tag without knowing the tag name?
Also how can i get the tag name as the data attribute?
For example:
i want to read different patients record. Every patient have different disease. Such as 'Heart Disease', 'cancer'.
<heart disease>serious</heart disease>
<cancer>normal</cancer>
I don't know both tags before. but i want to read the tag name and present it.
Finally, i can get data:
heart disease: serious
cancer normal:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,您可以,例如使用简单 XML:
Yes, you can, e.g. with Simple XML:
好吧,我觉得我应该首先警告您,空格不能构成有效的 XML 标记。您可以添加
STUFF
,但这将读取为
标记。使用更宽容的解释器,您可能会发现它有一个真正的space
布尔属性。将\s
替换为-
。使用 DOMDocument,要在不了解子节点(除了它们是子节点这一事实之外)的情况下读取子节点,您可以使用适当命名的
childNodes
属性:Well, I feel that I should first warn you that white-space does not make for valid XML tags. You can add
<white space>STUFF</white space>
, but that will read as a<white>
tag. With a more forgiving interpreter, you might find that it has a truespace
boolean attribute. Replace the\s
with-
.With DOMDocument, to read child nodes without knowing anything about them, other than the fact that they are child nodes, you would use the appropriately named
childNodes
property:我建议您将 XML 格式化为其他格式。以您知道的格式。像这样的事情:
然后您可以使用您的编程语言(在您的情况下为 PHP)中可用的每个解析器。
I suggest you format the XML into another format. In a format you DO know. Something like this:
Then you can use every parser that is available in your programming language (PHP in your case).