1

I tried to parse XML with code

import urllib2
from xml.etree import ElementTree

if __name__ == '__main__':
    print 'hello'
    result = urllib2.urlopen('http://localhost/conf.xml').read()
    xml = ElementTree.fromstring(result)
    print result
    print xml.findtext('.//type')

When I print resut I get all xml file and that is ok, but last line ( xml.findtext) always returns None ( I have tag with type and value mstp ). Can anybody help me with this ? I lloked at StackOverflow

How to parse xml in Python on Google App Engine but I dont get results with (.//type ).

There is xml file

<router>
  <datalink
    type="mstp"
    network="13"
    mac="18"
    hopcount="8">
    <mqueue
      name="/mstp1"
      msgnum="10"
      msgsize="768"
    />
    <mstp
      port="/dev/ttySx"
      baud="9600|19200|38400|76800"
      Nmax_info_frames="1+"
      Nmax_master="127-"
      Npoll="50"
      Nretry_token="1"
      Nmin_octets="4"
      Tframe_abort="60-100"
      Tframe_gap="20"
      Tno_token="500"
      Tpostdrive="15"
      Treply_delay="250"
      Treply_timeout="255-300"
      Troff="29-40"
      Tslot="10"
      Tturnaround="40"
      Tusage_delay="15"
      Tusage_timeout="20-100"
    />
  </datalink>
  <datalink
    type="bip"
    network="12"
    mac="192.168.0.146:47808"
    hopcount="8"
    >
    <mqueue
      name="/bip1"
      msgnum="10"
      msgsize="2048"
    />
    <bip
      bbmd="address|self|none"
      bmask="bmask"
    >
      <bbmd
    edit="yes|no"> <!-- dozvoljeno menjanje tabele -->
    <bdt address="192.168.0.131:0xBAC0:192.168.0.255"/> <!-- adresa:port:bmask -->
    <bdt address="192.168.0.157:0xBAC0:192.168.0.255"/>
      </bbmd>
    </bip>
  </datalink>
  <network
    unavailable="90%"
    available="40%"
    hop-dec="1">
    <mqueue
      name="/network"
      msgnum="40"
      msgsize="2048"
    />
    <!--  -->
    <hrpolicy
      general="ignore|activate|performance|demand"
      performance="num"
      conntime="num"
    />
  </network>
  <application>
    <mqueue
      name="/application"
      msgnum="10"
      msgsize="2048"
    />
  </application>
</router>
1
  • Can you provide the contents of the conf.xml file as well? Commented May 25, 2011 at 10:14

3 Answers 3

4

It's nothing to do with namespaces.

Problem 1: type is NOT a tag, it is an attribute of the element whose tag is datalink.

Problem 2: xml.findtext() returns the text component of the element; that's NOT what you want.

What you do want is this:

elem = xml.find(".//datalink")
print repr(elem)
print elem.get("type")

Output:

<Element 'datalink' at 0x019D0AB8>
mstp
Sign up to request clarification or add additional context in comments.

Comments

2

In your xml type is not an tag but an attribute. findtext('.//type') looks for a tag named type anywhere in your xml. if it finds,it returns the .text() of the tag. In your xml to get the type . you can do something like this

xml = ElementTree.fromstring(result)
datalink = xml.find('.//datalink')
type = datalink.get('type')

1 Comment

@John Machin. Sorry John , forgot to edit it. it should be find not findtext
1

elementtree has the nasty habit of using namespaced identifiers. i guess that your xml file has namespaces, so your search would have to look something like this:

print xml.findtext('.//{http://really-long-namespace.uri}type')

look at this question, there are some ways to cope with this.

/edit: i posted this answer back when the xml wasn’t provided in the question.

2 Comments

dumb question: you did replace the dummy namespace i used with the correct namespace the element has, did you?
-1 It's a good habit, not a nasty one (2) there is no namespace problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.