2

I have a Jboss host.xml file that has the effective structure of

<host xmlns="urn:jboss:domain:1.5">
...
    <domain-controller>
       <remote host="${jboss.domain.master.address}" port="${jboss.domain.master.port:9999}" security-realm="ManagementRealm"/>
    </domain-controller>
...
</host>

And am attempting to update the remote host using Etree based on the first parameter passed into the script, however using the following code, its not even been able to find the domain-controller stanza

import xml.etree.ElementTree as ET
import sys

newhost=sys.argv[1]

tree = ET.parse('host.xml').getroot()
print len(tree.findall('domain-controller'))
for elem in tree.findall('domain-controller'):
        print elem
    oldhost = elem.find('./remote').text
    print oldhosthost
    oldhost.set('host','newhost')

I've been able to find the correct element using minidom and getElementsByTagName, but would rather work with Etree as that seems to be the preferred approach.

Would someone mind telling me what I'm doing wrong?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
BenH
  • 45
  • 6
  • I think the "preferred approach" nowadays is to use `lxml` since it actually fully supports `XPath` and is stupidly fast to boot. – roippi May 18 '14 at 16:16
  • Thanks, but I'm limited to what's installed with the default RHEL packaging which I dont believe contains lxml – BenH May 18 '14 at 18:51

1 Answers1

2

since your XML lives in a namespace (xmlns), your tags have the be prefixed by the namespace:

for elem in tree.findall('{urn:jboss:domain:1.5}domain-controller'):

see this thread for alternative addressing of tags in namespaces when using ElementTree: Parsing XML with namespace in Python via 'ElementTree'

Community
  • 1
  • 1
Pavel
  • 7,436
  • 2
  • 29
  • 42