使用python lxml解析部分XML

编程入门 行业动态 更新时间:2024-10-08 18:39:48
本文介绍了使用python lxml解析部分XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试解析一个大型XML文件,该文件是使用Python从网络上接收的.

I'm trying to parse a large XML file which is being received from the network in Python.

为此,我获取了数据并将其传递给 lxml.etree.iterparse

In order to do that, I get the data and pass it to lxml.etree.iterparse

但是,如果XML尚未完全发送,就像这样:

However, if the XML has yet to fully be sent, like so:

<MyXML> <MyNode foo="bar"> <MyNode foo="ba

如果我运行 etree.iterparse(f,tag ='MyNode').next(),则无论它在什么地方被切断,我都会收到一个 XMLSyntaxError .

If I run etree.iterparse(f, tag='MyNode').next() I get an XMLSyntaxError at whereever it was cut off.

有什么办法可以使我收到第一个标签(即第一个MyNode),并且只有在到达文档的那一部分时才获得异常?(要使lxml真正地流式传输"内容,而在一开始不读取全部内容).

Is there any way I can make it so I can receive the first tag (i.e. the first MyNode) and only get an exception when I reach that part of the document? (To make lxml really 'stream' the contents and not read the whole thing in the beginning).

推荐答案

XMLPullParser 和 HTMLPullParser 可能会更好地满足您的需求.他们通过重复调用 parser.feed(data)来获取数据.在树可用之前,您仍然必须等待所有数据输入.

XMLPullParser and HTMLPullParser may better suite your needs. They get their data by repeated calls to parser.feed(data). You still have to wait until all of the data comes in before the tree is usable.

更多推荐

使用python lxml解析部分XML

本文发布于:2023-11-28 06:10:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641240.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:python   lxml   XML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!