我有一个xml文件,格式如下:
<stage rend="italic center" type="entrance"> aaaaa </stage>
<sp who="#F-mm-duk">bbbbb </sp>
<sp who="#smdwo">ccccc </sp>
<sp who="#F-mm-acb">cccc </sp>
<stage rend="italic center" type="entrance"> ddddd </stage>
如何从这些元素中提取文本以便: aaaaa级 bbbbb的 ccccc公司 滴滴涕
如果你不关心一些特殊情况,比如文本包含
'>', '</'
等可以使用一个正则表达式来提取所有节点的文本。你知道吗
代码如下:
输出:
但是更好的解决方案是使用一个类似xml.etree.elementtree的XML库,然后为节点挑选所有文本。你知道吗
代码如下:
一些XML库支持XPath(
xml.etree.elementtree
提供了有限的支持)。您可以在google上搜索它,然后研究如何用XPath实现相同的输出。你知道吗相关问题 更多 >
编程相关推荐