简体   繁体   English

在Python xml.dom中读取字符串而不进行解析

[英]Read string without parsing in Python xml.dom

As you probably know, Pythons xml.dom readString() throws an exception, if there is anything wrong with the xml code. 您可能知道,如果xml代码有任何问题,Pythons xml.dom readString()会抛出异常。

I am currently writing a simple xmpp chat server, and as you probably also know, one of the first client request message does not close the stream:stream tag, until it does not get a reply. 我目前正在编写一个简单的xmpp聊天服务器,并且您可能也知道,第一个客户端请求消息之一不会关闭stream:stream标记,直到它没有得到回复。

Is there any way to use pythons xml.dom without "parsing" the xmlcode or should I write my own xml parser? 有没有办法使用pythons xml.dom而不“解析”xmlcode或者我应该编写自己的xml解析器?

DOM parsers always need to read the entire XML file. DOM解析器总是需要读取整个XML文件。 You want a SAX parser . 你想要一个SAX解析器

Any reason you're not using an already established XMPP library like xmpppy ? 你有没有使用像xmpppy这样已经建立的XMPP库的原因 ( See also ) 另见

The parser that is likely to give you the best experience is xml.parsers.expat . 可能为您提供最佳体验的解析器是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. StartElementHandler创建一个DOM元素,并将其作为子元素添加到当前元素,然后将当前元素设置为新元素。 In your EndElementHandler , pop the current element to that element's parent. EndElementHandler ,将当前元素弹出到该元素的父元素。 If the parent was null , you have a stanza. 如果父级为null ,则表示您有一个节。 Do careful testing of namespaces; 仔细测试命名空间; you're liable to get the attributes wrong the first time. 你第一次有责任弄错属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM