我需要針對DTD描述文件驗證XML字符串(而不是文件)。怎么做python呢?
2 回答

不負相思意
TA貢獻1777條經驗 獲得超10個贊
從libxml2 python綁定中的examples目錄中:
#!/usr/bin/python -u
import libxml2
import sys
# Memory debug specific
libxml2.debugMemory(1)
dtd="""<!ELEMENT foo EMPTY>"""
instance="""<?xml version="1.0"?>
<foo></foo>"""
dtd = libxml2.parseDTD(None, 'test.dtd')
ctxt = libxml2.newValidCtxt()
doc = libxml2.parseDoc(instance)
ret = doc.validateDtd(ctxt, dtd)
if ret != 1:
print "error doing DTD validation"
sys.exit(1)
doc.freeDoc()
dtd.freeDtd()
del dtd
del ctxt
添加回答
舉報
0/150
提交
取消