-1

我有一个如下的sample.xml文件..我需要扫描项目“kernel/msm”的“revision”标签并在“refs/heads”之后打印这个词..我下面有一个sample.xml文件和xml输出?我以后可以弄清楚python部分,任何人都可以提供如何做到这一点的输入吗?

输入:-

Assume there is a variable project like below

project='kernel/msm'

sample.xml

    <?xml version="1.0" encoding="utf-8"?>
    <project name="platform/vendor/google/proprietary/code"
             path="vendor/widevine"
             revision="refs/heads/ab_mr"
             x-grease-customer="none"
             x-quic-dist="none"
             x-ship="none" />
    <!-- test Projects -->
    <project name="kernel/msm"
             path="kernel"
             revision="refs/heads/msm-3.4"
             x-grease-customer="none"
             x-quic-dist="la"
             x-ship="oss" />
    ......

    EXPECTED OUTPUT:-

    msm-3.4

Sample code:-

project='kernel/msm'
#open xml file
with open('./test.xml', 'r') as f:
    #get the branch and project
    for line in project :
        if line in 'revision':
            branch = line.split('/')[-1]
print branch

谢谢

4

1 回答 1

1
import xml.etree.ElementTree as ET
import re

temp = 'refs/heads/'
name = 'kernel/msm'

pattern = re.compile('%s(.*)' % temp)
tree = ET.parse('sample.xml')
root = tree.getroot()

project = root.find("./project[@name='%s']" % name)
revision = project.get('revision')

res = pattern.match(revision)
print(res.group(1))

您必须使用根节点包装您的 xml 数据,例如 <data> ,否则会引发解析错误。

于 2013-07-09T05:55:09.523 回答