lin提供的XML Python搜索对象

2024-10-03 15:33:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找使用Python2.7从xml文件中获取整个元素的方法,该方法基于发生错误的行号。你知道吗

http://pastebin.com/GjmPwg1a这里是我的xml文件的一个例子(在完整版本中有数百万行)。你知道吗

我需要得到<featureMember>标签之间的所有东西,但不知道如何实现这一点,所以任何帮助都将不胜感激。谢谢。你知道吗


Tags: 文件方法版本comhttp元素错误标签
1条回答
网友
1楼 · 发布于 2024-10-03 15:33:30

Python中有不同的XML解析方法。 例如,不安装lxml就可以使用minidom。你知道吗

示例代码为:

from urllib.request import urlopen
from xml.dom import minidom

# your address with raw xml output
address = "http://pastebin.com/raw/GjmPwg1a"

webPage = urlopen(address)
response = str(webPage.read(), encoding='utf-8')

tree = minidom.parseString(response)
nodes = tree.getElementsByTagName('gml:featureMember')

for node in nodes:
    print(node.toxml())

例如,只需将节点添加到处理中。你知道吗

当然,先附上你的代码会很有帮助。你知道吗

相关文档:http://www.diveintopython.net/xml_processing/parsing_xml.html

类似的主题也存在:只需在Google中搜索“stackoverflow解析xmlpython”。你知道吗

相关问题 更多 >