开发者

Read string without parsing in Python xml.dom

开发者 https://www.devze.com 2023-04-07 03:44 出处:网络
As you probably know, Pythons xml.dom readString() throws an exception, if there is anything wrong with the xml code.

As you probably know, Pythons xml.dom readString() throws an exception, if there is anything wrong with the xml code.

I am currently writing a simple xmpp chat server, and as you probably a开发者_开发知识库lso know, one of the first client request message does not close the stream:stream tag, until it does not get a reply.

Is there any way to use pythons xml.dom without "parsing" the xmlcode or should I write my own xml parser?


DOM parsers always need to read the entire XML file. You want a SAX parser.


Any reason you're not using an already established XMPP library like xmpppy? (See also)


The parser that is likely to give you the best experience is xml.parsers.expat. In your StartElementHandler create a DOM element, and add it to the current element as a child, then set the current element to the new element. In your EndElementHandler, pop the current element to that element's parent. If the parent was null, you have a stanza. Do careful testing of namespaces; you're liable to get the attributes wrong the first time.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号