2017-01-02 83 views
1

试图解析这个XML,但我似乎无法弄清楚我的错误。Python阅读XML儿童

段XML的:

<thexml timestamp="2017-01-02T10:17:41"> 
<event="41" date="2017-04-01" id="5543" time="09:30:00" type="seat" link="na"></event> 
</thexml> 

我想:

DOMTree = parseString(response.content) 
collection = DOMTree.documentElement 
selections = collection.getElementsByTagName("event") 
for select in selections: 
print "event found" 

这似乎是工作它触发在XML中的事件。例如,试图获得这种类型,这种格式让我感到困惑。

tags = select.getElementsByTagName("type") 

当我使用这个标签字符串变成暗示它找到它。但我不确定如何真正阅读孩子的字符串。我一直在尝试变化:

print type.childNodes[0].data 
print type.childNodes.data 
print type.data 

我在这里丢失了一些非常明显的东西吗?我解析了一堆XML,但是这种格式引起了我的一些注意。希望得到正确的方向。

+0

是您使用的库来解析XML? – snakecharmerb

+0

您提供的XML无效。 'ExpatError:格式不正确(无效标记):'你确定这是你试图解析的确切XML吗? – Dekel

+0

修复了XML片段中的错字对不起。我正在使用xml.dom.minidom – PoweredByCoffee

回答

1

您的xml仍然存在问题。

这里是修复(以及如何提取相关属性):

In [17]: c = """<thexml timestamp="2017-01-02T10:17:41"> 
    ...: <event date="2017-04-01" id="5543" time="09:30:00" type="seat" link="na"></event> 
    ...: </thexml> 
    ...: """ 
In [18]: DOMTree = parseString(c) 
In [19]: collection = DOMTree.documentElement 
In [20]: s = collection.getElementsByTagName('event') 
In [21]: for e in s: 
    ...:  print(e.getAttribute('type')) 
    ...: 
seat 

Note that in your example - type is an Attribute (And not Node) so you can't use getElementsByTagName("type") , you will need to use getAttribute

+0

哦,jeez我是个白痴。非常感谢你的赞赏。 – PoweredByCoffee

+0

当然:)不客气! – Dekel