如何从单个流解析多个 XML 文档? [英] How to parse multiple XML documents from a single stream?

查看:26
本文介绍了如何从单个流解析多个 XML 文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个套接字,我正在从中读取 XML 数据.但是,这个socket会吐出多个不同的XML文档,所以我不能简单地解析我收到的所有输出.

I've got a socket from which I'm reading XML data. However, this socket will spit out multiple different XML documents, so I can't simply parse all the output I receive.

有没有好办法,最好使用Python标准库,让我解析多个XML文档?换句话说,如果我最终得到

Is there a good way, preferably using the Python standard library, for me to parse multiple XML documents? In other words, if I end up getting

<foo/>
<bar/>

那么有没有办法要么获取多个 DOM 对象,要么让 SAX 解析器简单地处理这样的流?

then is there a way to either get multiple DOM objects or have a SAX parser simply work on such a stream?

推荐答案

如果你得到单独的文档,你需要一些东西来划分它们;如果有,您可以在解析单个文档之前简单地拆分流.

If you get separate documents, you'll need something to divide them; and if you have that, you can simply split the stream before you parse the individual documents.

另一种可能性是将其包装到另一个文档中,因此每个 XML 文档实际上都是您为此目的创建(并环绕)的父文档的子文档.

Another possibility would be to wrap that into another document, so each XML document is actually a subdocument of a parent's you create (and wrap around) just for that purpose.

这篇关于如何从单个流解析多个 XML 文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆