我刚开始使用python (3.x+),正在试图弄清楚如何从XML文件中提取所有元素,包括所有的子节点(例如孙子节点和伟大的孙子节点),而不需要在提取每个子节点之后进行检查。
我无法硬编码,因为xml文件可能会更改。我只想提取元素,它的父元素,以及它是否有子元素。
发布于 2015-05-10 07:49:51
您是否尝试过使用Feedparser模块并编写一个迭代的for循环,该循环可以深入到您希望提取的XML中的节点?
例如,您可以这样做:
import feedparser
d = feedparser.parse(r'c:\incoming\atom10.xml') #this is your locally hosted XML file
for index, post in enumerate(d.entries):
if index >= 5:
break
print(post.title) #replace 'title' with the name of the node you want in your XML file, etc
print(post.summary)
print(post.media_keywords)根据您希望在XML文件中提取的内容,您必须稍微使用一下脚本。有关更多详细信息,请查看documentation。
https://stackoverflow.com/questions/30144119
复制相似问题