美文网首页
2020-12-17:python-dom

2020-12-17:python-dom

作者: jsharmony | 来源:发表于2020-12-18 10:25 被阅读0次

    本文章通过python中dom接口解析xml文件:

    ##############xml文件格式:

    <?xml version="1.0"?>
    
    <one-level attr-one="top-level-first">
    
        <two-level attr-two="two-level-second-1">
    
            <three-level attr-three="three-level-third-1">
    
                <a>aaa-1</a>
    
                <b>bbb-1</b>
    
                <c>ccc-1</c>
    
            </three-level>
    
        </two-level>
    
        <two-level attr-two="two-level-second-2">
    
            <three-level attr-three="three-level-third-2">
    
                <a>aaa-2</a>
    
                <b>bbb-2</b>
    
                <c>ccc-2</c>
    
            </three-level>
    
        </two-level>
    
    </one-level>
    

    ##############python中dom解析代码:

    import xml.dom.minidom
    
    domtree = xml.dom.minidom.parse("movie-1-pro.xml")  # 解析成dom树
    
    collectionall = domtree.documentElement  # 获取树中元素
    
    # 获取树中顶层属性
    
    if collectionall.hasAttribute("attr-one"):
    
        print(collectionall.getAttribute("attr-one"))
    
    # 获取two-level下面元素内容
    
    twolevel = collectionall.getElementsByTagName("two-level")
    
    for each in twolevel:
    
        print("***********two-level***********")
    
        if each.hasAttribute("attr-two"):
    
            print("-%s" % each.getAttribute("attr-two"))
    
        # 获取three-level下面元素内容
    
        threelevel = each.getElementsByTagName("three-level")
    
        for cycle in threelevel:
    
            if cycle.hasAttribute("attr-three"):
    
                print("--%s" % cycle.getAttribute("attr-three"))
    
            a = cycle.getElementsByTagName("a")[0]
    
            print("---%s" % a.childNodes[0].data)
    
            b = cycle.getElementsByTagName("b")[0]
    
            print("---%s" % b.childNodes[0].data)
    
            c = cycle.getElementsByTagName("c")[0]
    
            print("---%s" % c.childNodes[0].data)
    

    ##############解析结果:

    top-level-first
    
    ***********two-level***********
    
    -two-level-second-1
    
    --three-level-third-1
    
    ---aaa-1
    
    ---bbb-1
    
    ---ccc-1
    
    ***********two-level***********
    
    -two-level-second-2
    
    --three-level-third-2
    
    ---aaa-2
    
    ---bbb-2
    
    ---ccc-2
    

    相关文章

      网友评论

          本文标题:2020-12-17:python-dom

          本文链接:https://www.haomeiwen.com/subject/xpmzgktx.html