在XML.Sax(Python)中不为我工作

发布于 2025-02-08 17:53:21 字数 1092 浏览 1 评论 0原文

我需要验证XML,但代码有变量(str),而不是来自文件。

因此,我认为使用XML.SAX很容易做到。但是我不能为我工作。解析文件时正常工作,但是解析字符串时会遇到一个奇怪的错误。

这是我的测试代码:

from xml.sax import make_parser, parseString
import os

filename = os.path.join('.', 'data', 'data.xml')
xmlstr = "<note>\n<to>Mary</to>\n<from>Jane</from>\n<heading>Reminder</heading>\n<body>Go to the zoo</body>\n</note>"


def parsefile(file):
    parser = make_parser()
    parser.parse(file)


def parsestr(xmlstr):
    parser = make_parser()
    parseString(xmlstr.encode('utf-8'), parser)


try:
    parsefile(filename)
    print("%s is well-formed" % filename)
except Exception as e:
    print("%s is NOT well-formed! %s" % (filename, e))


try:
    parsestr(xmlstr)
    print("%s is well-formed" % ('xml string'))
except Exception as e:
    print("%s is NOT well-formed! %s" % ('xml string', e))

执行脚本时,我明白了:

./data/data.xml is well-formed
xml string is NOT well-formed! 'ExpatParser' object has no attribute 'processingInstruction'

我缺少什么?

I need to validate xml but the code comes in a variable (str), not from a file.

So I figured this would be easy to do with xml.sax. But I can't get it to work for me. It works fine when parsing a file, but I get a strange error when parsing a string.

Here's my test-code:

from xml.sax import make_parser, parseString
import os

filename = os.path.join('.', 'data', 'data.xml')
xmlstr = "<note>\n<to>Mary</to>\n<from>Jane</from>\n<heading>Reminder</heading>\n<body>Go to the zoo</body>\n</note>"


def parsefile(file):
    parser = make_parser()
    parser.parse(file)


def parsestr(xmlstr):
    parser = make_parser()
    parseString(xmlstr.encode('utf-8'), parser)


try:
    parsefile(filename)
    print("%s is well-formed" % filename)
except Exception as e:
    print("%s is NOT well-formed! %s" % (filename, e))


try:
    parsestr(xmlstr)
    print("%s is well-formed" % ('xml string'))
except Exception as e:
    print("%s is NOT well-formed! %s" % ('xml string', e))

When executing the script, I get this:

./data/data.xml is well-formed
xml string is NOT well-formed! 'ExpatParser' object has no attribute 'processingInstruction'

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筑梦 2025-02-15 17:53:21

parsestring的第二个参数应该是 contenthandler 不是解析器。因为您正在传递错误类型的对象类型,所以它没有预期的方法。

您期望您将子类ContentHandler,然后根据需要处理SAX事件。在这种情况下,您实际上并不是要从文档中提取任何信息,因此您可以使用base contentHandler类:

from xml.sax import parseString, SAXParseException
from xml.sax.handler import ContentHandler

xmlstr = "<note>\n<to>Mary</to>\n<from>Jane</from>\n<heading>Reminder</heading>\n<body>Go to the zoo</body>\n</note>"

try:
    parseString(xmlstr, ContentHandler())
    print("document is well formed")
except SAXParseException as err:
    print("document is not well-formed:", err)

The second argument to parseString is supposed to be a ContentHandler, not a parser. Because you're passing in the wrong type of object, it doesn't have the expected methods.

You're expected to subclass ContentHandler and then handle the SAX events as necessary. In this case, you're not actually trying to extract any information from the document, so you could use the base ContentHandler class:

from xml.sax import parseString, SAXParseException
from xml.sax.handler import ContentHandler

xmlstr = "<note>\n<to>Mary</to>\n<from>Jane</from>\n<heading>Reminder</heading>\n<body>Go to the zoo</body>\n</note>"

try:
    parseString(xmlstr, ContentHandler())
    print("document is well formed")
except SAXParseException as err:
    print("document is not well-formed:", err)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文