使用Python 2或3在Python中进行XML解析

使用Python 2或3在Python中进行XML解析,python,Python,我只是想写一个简单的程序来解析下面的一些XML。 到目前为止,在下面的例子中,我没有得到我想要的结果 我遇到了许多这样的XML文件,我通常希望在一些标记之后获得这些信息。 使用elementtree搜索并获取标签中的任何信息的最佳方法是什么。我在尝试类似的事情 for Reel in root.findall('Reel'): ... id = Reel.findtext('Id') ... print id 是否有一种方法可以查找的每个实例并获取它后面的urn:etc?一些代

我只是想写一个简单的程序来解析下面的一些XML。 到目前为止,在下面的例子中,我没有得到我想要的结果

我遇到了许多这样的XML文件,我通常希望在一些标记之后获得这些信息。 使用elementtree搜索
并获取标签中的任何信息的最佳方法是什么。我在尝试类似的事情

for Reel in root.findall('Reel'):
...     id = Reel.findtext('Id')
...     print id
是否有一种方法可以查找
的每个实例并获取它后面的urn:etc?一些代码遍历所有内容并查找
等等

这是我通常处理的非常简略的版本

这根本没有得到我想要的。在任何XML文件中是否有一个简单的方法来匹配
,并获取该标记的内容,或者我是否需要充分了解XML的结构以了解它与根/子等的关系

 <Reel>
 <Id>urn:uuid:632437bc-73f9-49ca-b687-fdb3f98f430c</Id>
 <AssetList>
  <MainPicture>
   <Id>urn:uuid:46afe8a3-50be-4986-b9c8-34f4ba69572f</Id>
   <EditRate>24 1</EditRate>
   <IntrinsicDuration>340</IntrinsicDuration>
   <EntryPoint>0</EntryPoint>
   <Duration>340</Duration>
   <FrameRate>24 1</FrameRate>
   <ScreenAspectRatio>2048 858</ScreenAspectRatio>
  </MainPicture>
  <MainSound>
   <Id>urn:uuid:1fce0915-f8c7-48a7-b023-36e204a66ed1</Id>
   <EditRate>24 1</EditRate>
   <IntrinsicDuration>340</IntrinsicDuration>
   <EntryPoint>0</EntryPoint>
   <Duration>340</Duration>
  </MainSound>
 </AssetList>
</Reel>

urn:uuid:632437bc-73f9-49ca-b687-fdb3f98f430c
urn:uuid:46afe8a3-50be-4986-b9c8-34F4BA6957F
24 1
340
0
340
24 1
2048 858
urn:uuid:1fce0915-f8c7-48a7-b023-36e204a66ed1
24 1
340
0
340
@Mata工作得很好,但当我试图在另一个XML文件中使用它来表示不同的值时,我完全失败了。例如,一个文件的这一部分呢?不幸的是,我无法发布整个内容。如果我想抓住KeyId后面的东西呢

<?xml version="1.0" encoding="UTF-8" standalone="no" ?><DCinemaSecurityMessage xmlns="http://www.digicine.com/PROTO-ASDCP-KDM-20040311#" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" xmlns:enc="http://www.w3.org/2001/04/xmlenc#">
  <!-- Generated by Wailua Version 0.3.20 -->
  <AuthenticatedPublic Id="ID_AuthenticatedPublic">
    <MessageId>urn:uuid:7bc63f4c-c617-4d00-9e51-0c8cd6a4f59e</MessageId>
    <MessageType>http://www.digicine.com/PROTO-ASDCP-KDM-20040311#</MessageType>
    <AnnotationText>SPIDERMAN-3_FTR_S_EN-XX_US-13_51_4K_PH_20070423_DELUXE ~ KDM for Quvis-10010.pem</AnnotationText>
    <IssueDate>2007-04-29T04:13:43-00:00</IssueDate>
    <Signer>
      <dsig:X509IssuerName>dnQualifier=BzC0n/VV/uVrl2PL3uggPJ9va7Q=,CN=.deluxe-admin-c,OU=.mxf-j2c.ca.cinecert.com,O=.ca.cinecert.com</dsig:X509IssuerName>
      <dsig:X509SerialNumber>10039</dsig:X509SerialNumber>
    </Signer>
    <RequiredExtensions>
      <Recipient>
        <X509IssuerSerial>
          <dsig:X509IssuerName>dnQualifier=RUxyQle0qS7qPbcNRFBEgVjw0Og=,CN=SM.QuVIS.com.001,OU=QuVIS Digital Cinema,O=QuVIS.com</dsig:X509IssuerName>
          <dsig:X509SerialNumber>363</dsig:X509SerialNumber>
        </X509IssuerSerial>
        <X509SubjectName>CN=SM MD LE FM.QuVIS_CinemaPlayer-3d_10010,OU=QuVIS,O=QuVIS.com,dnQualifier=3oBfjTfx1me0p1ms7XOX\+eqUUtE=</X509SubjectName>
      </Recipient>
      <CompositionPlaylistId>urn:uuid:336263da-e4f1-324e-8e0c-ebea00ff79f4</CompositionPlaylistId>
      <ContentTitleText>SPIDERMAN-3_FTR_S_EN-XX_US-13_51_4K_PH_20070423_DELUXE</ContentTitleText>
      <ContentKeysNotValidBefore>2007-04-30T05:00:00-00:00</ContentKeysNotValidBefore>
      <ContentKeysNotValidAfter>2007-04-30T10:00:00-00:00</ContentKeysNotValidAfter>
      <KeyIdList>
        <KeyId>urn:uuid:9851b0f6-4790-0d4c-a69d-ea8abdedd03d</KeyId>
        <KeyId>urn:uuid:8317e8f3-1597-494d-9ed8-08a751ff8615</KeyId>
        <KeyId>urn:uuid:5d9b228d-7120-344c-aefc-840cdd32bbfc</KeyId>
        <KeyId>urn:uuid:1e32ccb2-ab0b-9d43-b879-1c12840c178b</KeyId>
        <KeyId>urn:uuid:44d04416-676a-2e4f-8995-165de8cab78d</KeyId>
        <KeyId>urn:uuid:906da0c1-b0cb-4541-b8a9-86476583cdc4</KeyId>
        <KeyId>urn:uuid:0fe2d73a-ebe3-9844-b3de-4517c63c4b90</KeyId>
        <KeyId>urn:uuid:862fa79a-18c7-9245-a172-486541bef0c0</KeyId>
        <KeyId>urn:uuid:aa2f1a88-7a55-894d-bc19-42afca589766</KeyId>
        <KeyId>urn:uuid:59d6eeff-cd56-6245-9f13-951554466626</KeyId>
        <KeyId>urn:uuid:14a13b1a-76ba-764c-97d0-9900f58af53e</KeyId>
        <KeyId>urn:uuid:ccdbe0ae-1c3f-224c-b450-947f43bbd640</KeyId>
        <KeyId>urn:uuid:dcd37f10-b042-8e44-bef0-89bda2174842</KeyId>
        <KeyId>urn:uuid:9dd7103e-7e5a-a840-a15f-f7d7fe699203</KeyId>
      </KeyIdList>
    </RequiredExtensions>
    <NonCriticalExtensions/>
  </AuthenticatedPublic>
  <AuthenticatedPrivate Id="ID_AuthenticatedPrivate"><enc:EncryptedKey xmlns:enc="http://www.w3.org/2001/04/xmlenc#">
<enc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p">
<ds:DigestMethod xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
</enc:EncryptionMethod>

urn:uuid:7bc63f4c-c617-4d00-9e51-0c8cd6a4f59e
http://www.digicine.com/PROTO-ASDCP-KDM-20040311#
蜘蛛侠-3_FTR_S_EN-XX_US-13_51_4K_PH_20070423_豪华~KDM用于Quvis-10010.pem
2007-04-29T04:13:43-00:00
dnQualifier=BzC0n/VV/uVrl2PL3uggPJ9va7Q=,CN=.deluxe-admin-c,OU=.mxf-j2c.ca.cinecert.com,O=.ca.cinecert.com
10039
dnQualifier=RUxyQle0qS7qPbcNRFBEgVjw0Og=,CN=SM.QuVIS.com.001,OU=QuVIS数字影院,O=QuVIS.com
363
CN=SM MD LE FM.QuVIS\u CinemaPlayer-3d\u 10010,OU=QuVIS,O=QuVIS.com,dnQualifier=3oBfjTfx1me0p1ms7XOX\+等式=
urn:uuid:336263da-e4f1-324e-8e0c-ebea00ff79f4
蜘蛛侠-3_FTR_S_EN-XX_US-13_51_4K_PH_20070423_豪华版
2007-04-30T05:00:00-00:00
2007-04-30T10:00:00-00:00
urn:uuid:9851b0f6-4790-0d4c-a69d-EA8ABDED03D
urn:uuid:8317e8f3-1597-494d-9ed8-08a751ff8615
urn:uuid:5d9b228d-7120-344c-aefc-840cdd32bbfc
urn:uuid:1e32ccb2-ab0b-9d43-b879-1c12840c178b
urn:uuid:44d04416-676a-2e4f-8995-165de8cab78d
urn:uuid:906da0c1-b0cb-4541-b8a9-86476583cdc4
urn:uuid:0fe2d73a-ebe3-9844-b3de-4517c63c4b90
urn:uuid:862fa79a-18c7-9245-a172-486541bef0c0
urn:uuid:aa2f1a88-7a55-894d-bc19-42afca589766
urn:uuid:59d6eeff-cd56-6245-9f13-951554466626
urn:uuid:14a13b1a-76ba-764c-97d0-9900f58af53e
urn:uuid:ccdbe0ae-1c3f-224c-b450-947f43bbd640
urn:uuid:dcd37f10-b042-8e44-bef0-89bda2174842
urn:uuid:9dd7103e-7e5a-a840-a15f-f7d7fe699203

这听起来像是你的拿手好戏——只要你知道结构,它就可以让你查询XML文档,准确地查找你要查找的内容。

表达式
Reel.findtext('Id')
只匹配
Reel
的直接子级。如果要在xml文档中查找所有
Id
标记,只需使用:

ids = [id.text for id in Reel.findall(".//Id")]
这将为您提供所有
Id
标记的所有文本节点的列表,这些标记是
revel
的子项


编辑: 您更新的示例使用名称空间,在本例中,
KeyId
位于默认名称空间(
http://www.digicine.com/PROTO-ASDCP-KDM-20040311#
),因此要搜索它,您需要将其包括在搜索中:

from xml.etree import ElementTree

doc = ElementTree.parse('test.xml')
nsmap = {'ns': 'http://www.digicine.com/PROTO-ASDCP-KDM-20040311#'}
ids = [id.text for id in doc.findall(".//ns:KeyId", namespaces=nsmap)]
print(ids)
...
xpath子集
ElementTree
所支持的功能相当有限。如果你想要一个更完整的支持,你应该使用它,它是更完整的方式

例如,使用xpath搜索所有
KeyId
标记(忽略名称空间)并直接返回它们的文本内容:

from lxml import etree
doc = etree.parse('test.xml')
ids = doc.xpath(".//*[local-name()='KeyId']/text()")
print(ids)
...

这是我需要做的。这能帮我找到我需要的东西

for node in tree.getiterator():
...     if 'KeyId' in node.tag:
...             mylist = node.tag
...             print(mylist)
... 

但是,假设我在XML文档中有一大堆Id字段,我想要Id后面的文本/数据。对不起,我觉得我对XML的了解不够,无法使用正确的vocab。@user1124541-我添加了更多示例,希望能有所帮助。顺便说一句,您的第二个xml片段无效,它缺少一些标记…@mata谢谢。我意识到我并不真正理解这些类型的xml文件的结构。我希望我可以发布所有的文件,以问更多的问题等。