lundi 13 juin 2016

Python: How do you use lxml to parse xml tags with periods?


I am attempting to parse Jenkin's job XML files using the lxml module for Python. It looks like this:

<triggers>
    <hudson.triggers.TimerTrigger>
       <spec>H H(6-21)/3 * * *</spec>
</hudson.triggers.TimerTrigger>

I like using lxml's handy objectify module, but it gets confused when I try to do this:

root.triggers.hudson.triggers.TimerTrigger.spec = 'something'

I get an AttributeError: no such child: hudson. Of course there's no attribute named hudson! How does one work with a goofy piece of XML like this?

For additional context, here is my code:

from lxml import objectify
import jenkins

j = jenkins.Jenkins('http://local.jenkins.instance')
xml = j.get_job_config('job_name')
root = objectify.fromstring(xml)
root.triggers.hudson.triggers.TimerTrigger.spec = 'something'

Aucun commentaire:

Enregistrer un commentaire