V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
woai110120130
V2EX  ›  Python

求教 这段 python 如何解析 本人 python 入门 只是用来写一些工具,这段 xml 试着解析还是得不到结果

  •  
  •   woai110120130 · 2016-07-28 09:58:47 +08:00 · 1972 次点击
    这是一个创建于 2800 天前的主题,其中的信息可能已经有所发展或是发生改变。
    <d:multistatus xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns">
    <d:response>
    <d:href>/sync/sms/016a92f5-bbba-4906-aee5-4d7df205ccfb.json</d:href>
    <d:propstat>
    <d:prop>
    <d:getlastmodified>2016-07-27 16:38:24</d:getlastmodified>
    <d:getetag>W/"c48f92b305e645047bc1f5a467603dfc"</d:getetag>
    </d:prop>
    <d:status>HTTP/1.1 200 OK</d:status>
    </d:propstat>
    </d:response>
    <d:response>
    <d:href>/sync/sms/02038c6a-8db7-48d1-8235-26bfc87c33ec.json</d:href>
    <d:propstat>
    <d:prop>
    <d:getlastmodified>2016-07-27 17:00:02</d:getlastmodified>
    <d:getetag>W/"ab299d2b5fd28ccb38472d871b95ab0c"</d:getetag>
    </d:prop>
    <d:status>HTTP/1.1 200 OK</d:status>
    </d:propstat>
    </d:response>
    <d:response>
    <d:href>/sync/sms/031968ec-89ec-40d8-a8ce-2db297dfd916.json</d:href>
    <d:propstat>
    <d:prop>
    <d:getlastmodified>2016-07-27 16:38:24</d:getlastmodified>
    <d:getetag>W/"4a0b083199697071b781992264dc935b"</d:getetag>
    </d:prop>
    <d:status>HTTP/1.1 200 OK</d:status>
    </d:propstat>
    </d:response>
    </d:multistatus>





    print "------clear -------------------------------------------------------"
    try:
    root = ET.fromstring(outstring)
    print "parse xml ok"
    except Exception, e:
    print "Error:cannot parse result."
    sys.exit(1)

    for child in root:
    print "tag:" ,child.tag, "---","attr:", child.attrib


    lst_node = root.getiterator("d:response")
    for node in lst_node:
    print "node.attrib:%s" % node.attrib
    print "node.tag:%s" % node.tag
    print "node.text:%s" % node.text



    我是想循环遍历
    <d:response>节点下的<d:href>   <d:getetag>的值 请高手指教下
    3 条回复    2016-07-28 10:54:16 +08:00
    holajamc
        1
    holajamc  
       2016-07-28 10:50:28 +08:00
    首先的得格式化一下 xml 文件。如下
    <d:multistatus xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns">

    <d:response>
    <d:href>/sync/sms/016a92f5-bbba-4906-aee5-4d7df205ccfb.json</d:href>
    <d:propstat>
    <d:prop>
    <d:getlastmodified>2016-07-27 16:38:24</d:getlastmodified>
    <d:getetag>W/"c48f92b305e645047bc1f5a467603dfc"</d:getetag>
    </d:prop>
    <d:status>HTTP/1.1 200 OK</d:status>
    </d:propstat>
    </d:response>

    <d:response>
    <d:href>/sync/sms/02038c6a-8db7-48d1-8235-26bfc87c33ec.json</d:href>
    <d:propstat>
    <d:prop>
    <d:getlastmodified>2016-07-27 17:00:02</d:getlastmodified>
    <d:getetag>W/"ab299d2b5fd28ccb38472d871b95ab0c"</d:getetag>
    </d:prop>
    <d:status>HTTP/1.1 200 OK</d:status>
    </d:propstat>
    </d:response>

    <d:response>
    <d:href>/sync/sms/031968ec-89ec-40d8-a8ce-2db297dfd916.json</d:href>
    <d:propstat>
    <d:prop>
    <d:getlastmodified>2016-07-27 16:38:24</d:getlastmodified>
    <d:getetag>W/"4a0b083199697071b781992264dc935b"</d:getetag>
    </d:prop>
    <d:status>HTTP/1.1 200 OK</d:status>
    </d:propstat>
    </d:response>

    </d:multistatus>

    大概是这样。 之后我简单的写了一部分,剩下的交给楼主~
    # -*- coding: utf-8 -*-
    import xml.etree.cElementTree as ET
    import sys
    reload( sys )
    sys.setdefaultencoding('utf-8')
    tree = ET.parse("xml")
    root = tree.getroot()

    for response in root:
    if response.tag != '{DAV:}response':
    continue
    pass
    else:
    for res in response:
    if res.tag == '{DAV:}href':
    print res.text
    pass
    else:
    pass
    pass
    holajamc
        2
    holajamc  
       2016-07-28 10:50:54 +08:00
    啪啪啪打脸= =
    holajamc
        3
    holajamc  
       2016-07-28 10:54:16 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3330 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 13:19 · PVG 21:19 · LAX 06:19 · JFK 09:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.