-1

我有以下 xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
    cq:lastReplicated="{Date}2016-03-02T15:23:40.679-05:00"
    cq:lastReplicatedBy="XXXXt"
    cq:lastReplicationAction="Activate"
    jcr:description="Procedure"
    jcr:mixinTypes="[cq:ReplicationStatus]"
    jcr:primaryType="cq:Tag"
    jcr:title="Lung Volume Reduction Surgery"
    sling:resourceType="cq/tagging/components/tag"/>

我正在尝试使用 ElementTree 解析 XML 文件,但我无法提取标签下的“Lung Volume Reduction Surgery” jcr:title

我已经尝试过 BeatifulSoup 、 Regex 和 ElementTree 但无法做到

下面是我用于 Element Tree 的代码:

import xml.etree.ElementTree as ET
xml="Actual xml document"
xml.find('./root').attrib['title']

我是 XML 解析的初学者 .. 现在在这个 XML 文件上花了 3 个多小时,但无法解析jcr:title任何帮助的值,将不胜感激

4

1 回答 1

1

这是一种方法,使用 xml.etree.ElementTree

from xml.etree import ElementTree as ET

tree = ET.parse('input.xml')
root = tree.getroot()

jcr_namespace = "http://www.jcp.org/jcr/1.0"

print root.attrib[ET.QName(jcr_namespace, 'title')]
于 2016-05-31T21:27:46.050 回答