我想查询一个数据库,然后将它返回到XML文档的类文件对象转换。这是我一直在做的事情:在Python中解析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: not well-formed (invalid token): line 1, column 4
差不多,如果我使用xml.etree.ElementTree做解析同样的事情发生。我认为他们都使用Expat。奇怪的部分是,如果不是在python中加载文件,而是将查询粘贴到Firefox中,则可以使用open(path_to_file,“r”)完美地读取生成的文件。
任何想法这可能是什么?
UPDATE: 这是文件的第一行:
<?xml version='1.0' encoding='UTF-8'?>
但是这可能不是什么在raw_result ...那是你下载查询result.srx和更改扩展名后会得到什么。文本。文件扩展名不重要吗?另外,我对这个完整的xml事物很新颖 - 为什么第4列是第8个字符? - Jeff 0秒前编辑
为什么不显示该XML的第一个8个字符? '再版(raw_result [:8])'。所以你会在第4列dontchathink看到究竟是什么困扰它 - - – 2010-07-15 05:56:36