0

I am going through tutorials on ElementList in Python with this file and all the operations work fine:

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>

I can find any bit of data I want in this, with the root being data. I got this here: http://docs.python.org/2/library/xml.etree.elementtree.html

Eg:

import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()

for child in root:
    print child.tag
    print child.attrib

This would give:

country
{'name': 'Liechtenstein'}
country
{'name': 'Singapore'}
country
{'name': 'Panama'}

However in the xml file I have to read and use I can't seem to get any of the data that I want. Here is the file: http://pastebin.com/b5bwrSFU

If I run the same code I get:

{http://clish.sourceforge.net/XMLSchema}VIEW
{'depth': '1', 'prompt': '${KHOSTNAME}(config-if)# ', 'name': 'configure-range-view'}
{http://clish.sourceforge.net/XMLSchema}VIEW
{'name': 'configure-view'}

I can't seem to get any data from below configure-view, any ideas?

I also tried:

for neighbor in root.iter('VIEW'):
    print neighbor.attrib

for i in root.findall('COMMAND'):
    print i
    rank = i.find('help').text
    name = i.get('name')
    print name, rank

and changing the root:

root = ET.Element("COMMAND")

Nothing gets printed out.

Paul
  • 5,756
  • 6
  • 48
  • 78
  • Your code does exactly the same thing on both xml documents: it goes through all immediate children of the root. – Marcin Jun 28 '13 at 15:28
  • Your XML contains namespaced tags; you need to qualify the namespace when using `findall()` or `iter()`. See the duped post. – Martijn Pieters Jun 28 '13 at 15:31
  • @Marcin So basically I have to search configure-view? – Paul Jun 28 '13 at 15:31
  • @MartijnPieters Thanks I'll ahve a look, so bsaically I qualify configure-view.find-all or somesuch – Paul Jun 28 '13 at 15:32
  • @Paul It's not clear to me what you want to do. But if you want to get information about the children of that element, you'll need to find it first. – Marcin Jun 28 '13 at 15:32
  • @Paul: See my answer there. – Martijn Pieters Jun 28 '13 at 15:33
  • @MartijnPieters Yes I want to be able to find the whole document, address each part, then I could write this whole file to a new file as an initial test. I just want to have everything addressable. Basically I'm trying to autogenerate code from existing code, so i need to be able to read it and choose what I want in a new file. I'm trying to understand your answer at the moment, thank you. – Paul Jun 28 '13 at 15:40
  • @MartijnPieters Trying it out but can't seem to print out anything still, will have another look. I need to do a lot more reading. `namespaces = {'configure-view'} print root.findall('COMMAND', namespaces=namespaces)` – Paul Jun 28 '13 at 15:50
  • 1
    Your `namespaces` dictionary is a set, not a dictionary. You need to set a value: `namespaces = {'c': 'http://clish.sourceforge.net/XMLSchema'}` associates the prefix `c` with the namespace I see in your post. – Martijn Pieters Jun 28 '13 at 16:20
  • @MartijnPieters Ah I'm not really getting this, whether I use c or not I cant find any data. And c.findall gives an error etc `namespaces = {'c': 'http://clish.sourceforge.net/XMLSchema'} print root.findall('c:configure-view', namespaces=namespaces) for i in root.findall('c:name', namespaces=namespaces): print i` – Paul Jul 01 '13 at 09:31
  • May I ask, if I wanted to print out `interface range` from my file, what is the syntax? – Paul Jul 01 '13 at 09:40

0 Answers0