我有一个 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 等,我想将这些 dat 插入数据库....任何人都可以帮助我吗?
Rgds,
尼米