Python3提取xml文件中的内容

import  xml.dom.minidom

def find_child(Par_nodes, mystr):
    for child_node in Par_nodes:
        if(len(child_node.childNodes) > 0):
            mystr = find_child(child_node.childNodes, mystr)
        elif(child_node.nodeValue != None):
            mystr += child_node.data.replace('\n', '')
    return mystr

if __name__ == '__main__':

    dom1 = xml.dom.minidom.parse('2.XML')  #打开xml文件
    root = dom1.documentElement         #得到文档元素对象
    app_nums = root.getElementsByTagName('base:DocNumber')  #按标签名称查找,返回标签结点数组
    app_num = app_nums[2]
    print('专利申请号:'+app_num.firstChild.data)
    titles = root.getElementsByTagName('business:InventionTitle')
    title = titles[0]
    print('专利名称:'+title.firstChild.data)
    Paragraphs = root.getElementsByTagName('base:Paragraphs')
    abstract = Paragraphs[0]
    print('专利摘要:'+abstract.firstChild.data)
    company_names = root.getElementsByTagName('base:Name')
    company_name = company_names[0]
    print('公司名称:'+company_name.firstChild.data)
    mystr = ''
    for i in range(len(Paragraphs)):
        if (Paragraphs[i].firstChild.data == '发明内容\n\t'):
            i+=1
            while Paragraphs[i].firstChild.data != '附图说明\n\t':
                mystr = find_child(Paragraphs[i].childNodes, mystr)
                i+=1

    print('发明内容:' + mystr)
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值