我有一個xml文件。 它的樣子,使用元素樹讀取xml文件
<root>
<Group>
<ChapterNo>1</ChapterNo>
<ChapterName>A</ChapterName>
<Line>1</Line>
<Content>zfsdfsdf</Content>
<Synonyms>fdgd</Synonyms>
<Translation>assdfsdfsdf</Translation>
</Group>
<Group>
<ChapterNo>1</ChapterNo>
<ChapterName>A</ChapterName>
<Line>2</Line>
<Content>ertreter</Content>
<Synonyms>retreter</Synonyms>
<Translation>erterte</Translation>
</Group>
<Group>
<ChapterNo>2</ChapterNo>
<ChapterName>B</ChapterName>
<Line>1</Line>
<Content>sadsafs</Content>
<Synonyms>sdfsdfsd</Synonyms>
<Translation>sdfsdfsd</Translation>
</Group>
<Group>
<ChapterNo>2</ChapterNo>
<ChapterName>B</ChapterName>
<Line>2</Line>
<Content>retete</Content>
<Synonyms>retertret</Synonyms>
<Translation>retertert</Translation>
</Group>
</root>
我這樣試過.......
root = ElementTree.parse('data.xml').getroot()
ChapterNo = root.find('ChapterNo').text
ChapterName = root.find('ChapterName').text
GitaLine = root.find('Line').text
Content = root.find('Content').text
Synonyms = root.find('Synonyms').text
Translation = root.find('Translation').text
但它顯示了一個錯誤
ChapterNo=root.find('ChapterNo').text
AttributeError: 'NoneType' object has no attribute 'text'`
現在我想要得到的一切ChapterNo,ChapterName等分別使用元素樹,我想將這些數據插入數據庫....任何人都可以幫助我?
RGDS,
Nimmy
我試過......... root = ElementTree.parse('data.xml')。getroot() ChapterNo = root.find('ChapterNo')。text ChapterName = root.find( 'ChapterName')。text GitaLine = root.find('Line')。text Content = root.find('Content')。text 同義詞= root.find('Synonyms')。text Translation = root。文本 AttributeError:'NoneType'對象沒有屬性'text'「 – Nimmy 2011-02-01 10:02:19
將其添加到您的問題中,其'hard'('Translation')。但是顯示錯誤」ChapterNo = root.find('ChapterNo'閱讀評論。 – 2011-02-01 10:03:11