我的.xml文件的文件夾,看起來像這樣:的Python解析XML並保存爲txt
<PubmedArticleSet>
<PubmedArticle>
<MedlineCitation Owner="NLM" Status="MEDLINE">
<PMID Version="1">23458631</PMID>
<DateCreated>
<Year>2013</Year>
<Month>04</Month>
<Day>08</Day>
</DateCreated>
<MeshHeadingList>
<MeshHeading>
<DescriptorName MajorTopicYN="N">Animals</DescriptorName>
</MeshHeading>
<MeshHeading>
<DescriptorName MajorTopicYN="N">Calcium</DescriptorName>
<QualifierName MajorTopicYN="Y">metabolism</QualifierName>
</MeshHeading>
<MeshHeading>
<DescriptorName MajorTopicYN="N">Calcium Chloride</DescriptorName>
<QualifierName MajorTopicYN="N">administration & dosage</QualifierName>
</MeshHeading>
</MeshHeadingList>
</MedlineCitation>
</PubmedArticle>
<PubmedArticle>
<MedlineCitation Status="Publisher" Owner="NLM">
<PMID Version="1">23458629</PMID>
<DateCreated>
<Year>2013</Year>
<Month>3</Month>
<Day>20</Day>
</DateCreated>
<MeshHeadingList>
<MeshHeading>
<DescriptorName MajorTopicYN="N">Adolescent</DescriptorName>
</MeshHeading>
<MeshHeading>
<DescriptorName MajorTopicYN="N">Adult</DescriptorName>
</MeshHeading>
<MeshHeading>
<DescriptorName MajorTopicYN="N">Anthropometry</DescriptorName>
</MeshHeading>
</MeshHeadingList>
</MedlineCitation>
</PubmedArticle>
</PubmedArticleSet>
我想使用Python來解析XML文件,並提取PMID,dateCreated會,所有DescriptorName和MajorTopicYN爲每篇文章。然後,該結果保存爲txt文件,看起來像:
ArticleID|CreatedDate|MeSH|IsMajor
23458631|20130408|Animals|N
23458631|20130408|Calcium|N
23458631|20130408|Calcium Chloride|N
23458629|20130320|Adolescent|N
23458629|20130320|Adult|N
23458629|20130320|Anthropometry|N
看一看HTTP: //stackoverflow.com/questions/1912434/how-do-i-parse-xml-in-python –