【发布时间】:2011-03-16 05:33:01
【问题描述】:
我正在尝试查询数据库,然后将它返回的类文件对象转换为 XML 文档。这是我一直在做的事情:
>>> import urllib, xml.dom.minidom
>>> query = "http://sbol.bhi.washington.edu/openrdf-sesame/repositories/sbol_test?query=select%20distinct%20%3Fname%20%3Ffeaturename%20where%20%7B%3Fpart%20%3Chttp%3A%2F%2Fsbol.bhi.washington.edu%2Frdf%2Fsbol.owl%23annotation%3E%20%3Fannotation%3B%3Chttp%3A%2F%2Fsbol.bhi.washington.edu%2Frdf%2Fsbol.owl%23status%3E%20'Available'%3B%3Chttp%3A%2F%2Fsbol.bhi.washington.edu%2Frdf%2Fsbol.owl%23name%3E%20%3Fname.%3Fannotation%20%3Chttp%3A%2F%2Fsbol.bhi.washington.edu%2Frdf%2Fsbol.owl%23feature%3E%20%3Ffeature.%3Ffeature%20%3Chttp%3A%2F%2Fwww.w3.org%2F1999%2F02%2F22-rdf-syntax-ns%23type%3E%20%3Chttp%3A%2F%2Fsbol.bhi.washington.edu%2Frdf%2Fsbol.owl%23binding%3E%3B%3Chttp%3A%2F%2Fsbol.bhi.washington.edu%2Frdf%2Fsbol.owl%23name%3E%20%3Ffeaturename%7D"
>>> raw_result = urllib.urlopen(query)
>>> xml_result = xml.dom.minidom.parse(raw_result)
最后一条命令给了我
xml.parsers.expat.ExpatError:格式不正确(无效标记):第 1 行,第 4 列如果我使用 xml.etree.ElementTree 进行解析,几乎会发生同样的事情。我认为他们都使用Expat。奇怪的是,如果不是在 python 中加载文件,而是将查询粘贴到 Firefox 中,则可以使用 open(path_to_file, "r") 很好地读取结果文件。
有什么想法吗?
更新: 这是文件的第一行:
<?xml version='1.0' encoding='UTF-8'?>
但这可能不是 raw_result 中的内容...这是您在下载 query-result.srx 并将扩展名更改为 .txt 后得到的内容。文件扩展名无关紧要吗?另外,我对整个 xml 的东西很陌生——为什么第 4 列是第 8 个字符? – Jeff 0 秒前编辑
【问题讨论】:
-
为什么不显示该 XML 的前 8 个字符?
repr(raw_result[:8])。所以你会在第 4 栏看到究竟是什么困扰着它,别想了?-)
标签: python xml xmlhttprequest