如何使用SimpleXML解析XML以获取多个文本块? [英] How can I parse XML to get multiple text blocks with SimpleXML?
本文介绍了如何使用SimpleXML解析XML以获取多个文本块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想解析一些看起来像这样的XML:
I want to parse some XML that looks like this:
<node>
This is
<child>
blah
</child>
some
<child>
foo
</child>
text
</node>
如何访问Simple XML中的文本节点子级?
How do I get access to the text node children in Simple XML?
我可以按文本和元素子代的正确顺序访问它们吗?
Can I access them in the correct order of text and element children?
我需要其他包装吗?
推荐答案
我强烈建议切换到 DOM函数.不久前,我有这样的答案非常受欢迎,但我仍然坚持. DOM功能是如此强大:额外的冗长是值得的.
I'd strongly recommend switching to the DOM functions over SimpleXML. I had an answer like this a while ago which wasn't very popular, but I still stand by it. The DOM functions are just so much powerful: the extra verbosity is worth it.
$doc = new DOMDocument();
$doc->loadXML($xmlString);
foreach ($doc->documentElement->childNodes as $node) {
if ($node->nodeType === XML_TEXT_NODE) {
echo $node->nodeValue . "\n";
}
}
这篇关于如何使用SimpleXML解析XML以获取多个文本块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文