简体   繁体   English

为什么ElementTree无法正确解析此XML标签?

[英]Why ElementTree can't parse this XML tag correctly?

The XML file looks like this: XML文件如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<MINiML
   xmlns="http://www.ncbi.nlm.nih.gov/geo/info/MINiML"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.ncbi.nlm.nih.gov/geo/info/MINiML http://www.ncbi.nlm.nih.gov/geo/info/MINiML.xsd"
   version="0.5.0" >

  <Contributor iid="contrib1">
    <Person><First>ENCODE</First><Last>DCC</Last></Person>
    <Email>encode-help@lists.stanford.edu</Email>
    <Organization>ENCODE DCC</Organization>
    <Address>
      <Line>300 Pasteur Dr</Line>
      <City>Stanford</City>
      <State>CA</State>
      <Zip-Code>94305-5120</Zip-Code>
      <Country>USA</Country>
    </Address>
  </Contributor>
</MINiML>

Here is how I use the ElementTree in Python: 这是我在Python中使用ElementTree的方法:

import xml.etree.cElementTree as ET
tree=ET.parse("the_file_above.xml")
root = tree.getroot()
for c in root:
    print c.tag, c.attrib

It returns: 它返回:

{http://www.ncbi.nlm.nih.gov/geo/info/MINiML}Contributor {'iid': 'contrib1'}

And the value of c.tag is '{http://www.ncbi.nlm.nih.gov/geo/info/MINiML}Contributor' , which I expected to be Contributor . 而且c.tag的值是'{http://www.ncbi.nlm.nih.gov/geo/info/MINiML}Contributor' Contributor '{http://www.ncbi.nlm.nih.gov/geo/info/MINiML}Contributor' ,我希望可以成为Contributor I'm not sure how the long url mixed in the tag. 我不确定长网址如何混入标签中。 Does anyone have ideas about this? 有人对此有想法吗?

This library (like others) tries to encode the namespace used into the tag names. 该库(与其他库一样)试图将使用的名称空间编码为标签名称。 This is done by prepending the namespace in curly braces. 这是通过将名称空间放在花括号中来完成的。 So this is just a feature you might not be aware of and which might be cumbersome to you. 因此,这只是您可能不知道的功能,可能对您来说很麻烦。 But it is necessary in case you mix XMLs from various sources with various name spaces in which otherwise tag names would clash. 但是,如果将来自不同来源的XML与不同的名称空间混合在一起,则很有必要,否则标签名称将发生冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM