gpt4 book ai didi

Python XML 需要有关编程错误的帮助

转载 作者:行者123 更新时间:2023-12-01 06:17:35 26 4
gpt4 key购买 nike

我有以下代码。

import xml.dom.minidom

def get_a_document(name):
return xml.dom.minidom.parse(name)

doc = get_a_document("sources.xml")

sources = doc.childNodes[1]

for e in sources.childNodes:
if e.nodeType == e.ELEMENT_NODE and e.localName == "source":
for source in e.childNodes:
print source.localName
print source.nodeType
if source.nodeType == source.ELEMENT_NAME and source.localName == "language":
print source.localName
country = doc.createElement("country")
e.appendChild(country)

我正在尝试读取sources.xml并添加一个元素国家/地区。但是,我收到以下错误。

AttributeError: Text instance has no attribute 'ELEMENT_NAME'

Sources.xml 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<!--sources.xml for multilingual, follows an ID range for different type of sources. Dailies sources are merged to this list-->
<sources xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<source>
<id>1005001</id>
<language>Afar</language>
<status>active</status>
<tags>
<tag>language</tag>
</tags>
<title>Afar</title>
</source>
</sources>

有人还可以推荐一个关于 minidom 库的好教程吗?另外,如果您能推荐一个更好的 python xml 库,那就太好了。

谢谢巴拉

最佳答案

可能发生的情况是您遇到了标签之间包含空格的节点。目前尚不清楚您要做什么,但如果您仅删除 source.nodeType == source.ELEMENT_NAME 部分,它可能会起作用。

关于Python XML 需要有关编程错误的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2391037/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com