0
import xml.dom.minidom

content = """
<urlset xmlns="http://www.google.com/schemas/sitemap/0.90">
  <url>
    <loc>http://www.domain.com/</loc>
    <lastmod>2011-01-27T23:55:42+01:00</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.5</priority>
  </url>
  <url>
    <loc>http://www.domain.com/page1.html</loc>
    <lastmod>2011-01-26T17:24:27+01:00</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.5</priority>
  </url>  
  <url>
    <loc>http://www.domain.com/page2.html</loc>
    <lastmod>2011-01-26T15:35:07+01:00</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.5</priority>
  </url>  
</urlset>
"""

xml = xml.dom.minidom.parseString(content)
urlset = xml.getElementsByTagName("urlset")[0]
url = urlset.getElementsByTagName("url")

for i in range(0, url.length):
    loc = url[i].getElementsByTagName("loc")[0].childNodes[0].nodeValue
    lastmod = url[i].getElementsByTagName("lastmod")[0].childNodes[0].nodeValue
    changefreq = url[i].getElementsByTagName("changefreq")[0].childNodes[0].nodeValue
    priority = url[i].getElementsByTagName("priority")[0].childNodes[0].nodeValue
    print "%s, %s, %s, %s" % (loc, lastmod, changefreq, priority)

没有更简单的方法来获取节点的值吗?

loc = url[i].getElementsByTagName("loc")[0].childNodes[0].nodeValue
4

4 回答 4

0

这行得通吗:loc = getElementsByTagName("loc")[i].innerHTML

于 2012-08-03T07:16:33.033 回答
0

可能有更好的方法来获取节点的值......但这至少是一个更干净的选择,您不会重复自己:

import xml.dom.minidom

content = """
<urlset xmlns="http://www.google.com/schemas/sitemap/0.90">
  <url>
    <loc>http://www.domain.com/</loc>
    <lastmod>2011-01-27T23:55:42+01:00</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.5</priority>
  </url>
  <url>
    <loc>http://www.domain.com/page1.html</loc>
    <lastmod>2011-01-26T17:24:27+01:00</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.5</priority>
  </url>  
  <url>
    <loc>http://www.domain.com/page2.html</loc>
    <lastmod>2011-01-26T15:35:07+01:00</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.5</priority>
  </url>  
</urlset>
"""

def get_first_node_val(obj, tag):
    return obj.getElementsByTagName(tag)[0].childNodes[0].nodeValue

xml = xml.dom.minidom.parseString(content)
urlset = xml.getElementsByTagName("urlset")[0]
urls = urlset.getElementsByTagName("url")

for url in urls:
    loc = get_first_node_val(url, "loc")
    lastmod = get_first_node_val(url, "lastmod")
    changefreq = get_first_node_val(url, "changefreq")
    priority = get_first_node_val(url, "priority")
    print "%s, %s, %s, %s" % (loc, lastmod, changefreq, priority)
于 2012-08-03T07:25:42.783 回答
0

为什么不是节点的第一个孩子

loc = url[i].getElementsByTagName("loc").firstChild.nodeValue
于 2012-08-03T07:26:56.273 回答
0

向“get_first_node_val”添加附加功能,它接受具有相同节点值的 XML 元素。例如,以下包含两个 loc 元素。

<url>
<loc>http://domain.com/</loc>
<loc>http://sub.domain.com</loc>
<lastmod>2011-01-27T23:55:42+01:00</lastmod>
<changefreq>daily</changefreq>
<priority>0.5</priority>
</url>


def get_first_node_val(obj, tag):
  element = []
  l = 0
  for x in obj.getElementsByTagName(tag):
    element.append({tag : obj.getElementsByTagName(tag)[l].childNodes[0].nodeValue})
    l += 1
  return element

输出

[{'loc': u'http://domain.com/'}, {'loc': u'http://sub.domain.com'}], [{'lastmod': u'2011-01-27T23:55:42+01:00'}], [{'changefreq': u'daily'}], [{'priority': u'0.5'}]
于 2014-11-29T18:55:34.203 回答