1

First question. If I screwed up somehow let me know.

Ok, what I need to do is the following. I'm trying to use Python to get some data from an API. The API sends it to me in XML. I'm trying to use ElementTree to parse it.

Now every time I request information from the API, it's different. I want to construct a list of all the data I get. I could use Python's lists, but since I want to save it to a file at the end I figured - why not use ElementTree for that too.

Start with an Element, lets call it ListE. Call the API, parse the XML, get the root Element from the ElementTree. Add the root Element as a subelement into ListE. Call the API again, and do it all over. At the end ListE should be an Element whose subelements are the results of each API call. And the end of everything just wrap ListE into an ElementTree in order to use the ElementTree write() function. Below is the code.

import xml.etree.ElementTree as ET

url = "http://http://api.intrade.com/jsp/XML/MarketData/ContractBookXML.jsp?id=769355"

try:
    returnurl=urlopen(url)
except IOError:
    exit()

tree = ET.parse(returnurl)
root = tree.getroot()

print "root tag and attrib: ",root.tag, root.attrib

historyE = ET.Element('historical data')
historyE.append(root)
historyE.append(root)

historyET = ET.ElementTree(historyE)
historyET.write('output.xml',"UTF-8")

The program doesn't return any error. The problem is when I ask the browser to open it, it claims a syntax error. Opening the file with notepad here's what I find:

<?xml version='1.0' encoding='UTF-8'?>
<historical data><ContractBookInfo lastUpdateTime="0">
   <contractInfo conID="769355" expiryPrice="100.0" expiryTime="1357334563000" state="S" vol="712" />
</ContractBookInfo><ContractBookInfo lastUpdateTime="0">
   <contractInfo conID="769355" expiryPrice="100.0" expiryTime="1357334563000" state="S" vol="712" />
</ContractBookInfo></historical data>

I think the reason for the syntax error is that there isn't a space or a return between 'historical data' and 'ContractBookInfo lastUpdateTime="0"'. Suggestions?

4

1 に答える 1

3

問題はここにあります:

historyE = ET.Element('historical data')

スペースを使用しないでください。ウィキペディアに要約されているように:

要素タグは大文字と小文字が区別されます。開始タグと終了タグは正確に一致する必要があります。タグ名には、!"#$%&'()*+、/;<=>?@[]^`{|}~、スペース文字を含めることはできず、-、.、または数字。

詳細については、XML 仕様のこのセクションを参照してください (「ほとんどすべての文字が名前に使用できますが、区切り文字として使用されるか、合理的に使用できる文字は除きます。」)

于 2013-01-05T02:33:03.423 に答える