相关疑难解决方法(0)

如何使用ElementTree以递归方式迭代Python中的XML标签?

我试图使用ElementTree迭代树中的所有节点.

我做的事情如下:

  tree = ET.parse("/tmp/test.xml")

  root = tree.getroot()

  for child in root:
       ### do something with child
Run Code Online (Sandbox Code Playgroud)

问题是child是一个Element对象而不是ElementTree对象,所以我无法进一步查看它并递归迭代它的元素.有没有办法在"root"上进行不同的迭代,以便迭代树中的顶级节点(直接子节点)并返回与root本身相同的类?

python xml

15
推荐指数
4
解决办法
4万
查看次数

OSError:[Errno 36]文件名太长:

我需要将网页转换为XML(使用Python 3.4.3).如果我将URL的内容写入文件,那么我可以完美地阅读和解析它,但如果我尝试直接从网页上读取,我的终端中会出现以下错误:

文件"./AnimeXML.py",第22行,在xml = ElementTree.parse(xmlData)文件"/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/xml/etree/ElementTree.py" ,第1187行,在parse tree.parse(source,parser)文件"/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/xml/etree/ElementTree.py",第587行,在解析源中= open(source,"rb")OSError:[Errno 36]文件名太长:

我的python代码:

# AnimeXML.py
#! /usr/bin/Python

# Import xml parser.
import xml.etree.ElementTree as ElementTree

# XML to parse.
sampleUrl = "http://cdn.animenewsnetwork.com/encyclopedia/api.xml?anime=16989"

# Read the xml as a file.
content = urlopen (sampleUrl)

# XML content is stored here to start working on it.
xmlData = content.readall().decode('utf-8')

# Close the file.
content.close()

# Start parsing XML.
xml = ElementTree.parse (xmlData)

# Get root of the XML file.
root = xml.getroot()

for info …
Run Code Online (Sandbox Code Playgroud)

python xml

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

python ×2

xml ×2