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

2016-07-28 09:58:47 +08:00
 woai110120130
<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>的值 请高手指教下
1993 次点击
所在节点    Python
3 条回复
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
2016-07-28 10:50:54 +08:00
啪啪啪打脸= =
holajamc
2016-07-28 10:54:16 +08:00

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/295421

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX