gpt4 book ai didi

根据子文本节点从大 XML 中提取子 XML 的 Java 或 Python 方法

转载 作者:行者123 更新时间:2023-12-01 09:34:25 24 4
gpt4 key购买 nike

我有一个大的 XML 需要处理,我需要提取所有“情况”标签,如果这些标签具有 <_0:roadNumber>A-52、<_0:roadNumber>AP-9 或 <_0:roadNumber>A-他们的值有 55 个,因为我不需要 XML 的其余部分。然后使用 XML 减法构建 XML 文档。我不需要实现,只是我想知道如何处理这个或者哪个 API 是最合适的,谢谢。

PD:我的最终目标是将 XML 转储到数据库中

XML GET:

print("GETTING XML...")
resp = requests.get('http://infocar.dgt.es/datex2/dgt/SituationPublication/all/content.xml', stream = True) #XML that I need
if resp.status_code != 200:
raise ApiError('GET /tasks/ {}'.format(resp.status_code))
print("XML RECIBIDO 200 OK")
#resp.raw.decode_content = True
print("GUARDANDO XML")
with open("DGT_DATEX.xml", "wb") as handle:
for data in (resp.iter_content()):
handle.write(data)

dom = parse("DGT_DATEX.xml")

最佳答案

对于非常大的 XML 文档,您最好使用 SAX用于流式传输(不需要立即将完整文档存储在内存中),但用于轻松查找元素 XPath真的很有帮助。

对于 Python,你有一些 XPath supportxml.etree.ElementTree和 SAX xml.sax - 但当然还有其他解析器。

还有 SAX 实现和 Java 的 XPath。

关于根据子文本节点从大 XML 中提取子 XML 的 Java 或 Python 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39123136/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com