1

I have an xml file:

    <movie title="Enemy Behind">
       <type>War, Thriller</type>
       <type>WW2</type>
       <format>DVD</format>
       <year>2003</year>
       <rating>PG</rating>
       <stars>10</stars>
       <description>Talk about a US-Japan war</description>
   </movie>

And I am using the following code to Parse this XML in Python:

     Print detail of each movie.
     for movie in movies:
        print ("*****Movie*****")
        if movie.hasAttribute("title"):
           print ("Title: %s" % movie.getAttribute("title"))

        type = movie.getElementsByTagName('type')[0]
        print ("Type: %s" % type.childNodes[0].data)
        format = movie.getElementsByTagName('format')[0]
        print ("Format: %s" % format.childNodes[0].data)
        rating = movie.getElementsByTagName('rating')[0]
        print ("Rating: %s" % rating.childNodes[0].data)
        description = movie.getElementsByTagName('description')[0]
        print ("Description: %s" % description.childNodes[0].data)

But using this code only one of the attribute gets printed i.e. "War, Thriller". The other attribute that says "WW2" doesn't get printed.

Should I use a for loop? I have tried that and I get an error "'Element' object is not iterable".

1
  • What does this show? print type(movie.getElementsByTagName('type')) Commented Dec 7, 2016 at 2:29

1 Answer 1

3

I don't know what library you are using but you can get the values for the XML snippet with the code below:

test.xml

   <movie title="Enemy Behind">
       <type>War, Thriller</type>
       <type>WW2</type>
       <format>DVD</format>
       <year>2003</year>
       <rating>PG</rating>
       <stars>10</stars>
       <description>Talk about a US-Japan war</description>
   </movie>

test.py

import lxml.etree

# Getting the XML root tag... Movie in our case
root = lxml.etree.parse("test.xml").getroot()

# the method "get" returns the value of the XML attribute informed
# as parameter
print(root.get("title"))

# You can iterate over the children of an XML node
for child in root:
    print(child.text) # gets the text value from the children XML nodes

# Or more specifically, for each type, use the method FIND to get
# the XML child node from the current XML node.
node = root.find("name")
if node is not None:
    print(node.text)

# ..Or if you expect more than one node, as it is the case for the tag
# "type", you can use FINDALL which returns all direct children from the current XML node.
nodes = root.findall("type")
for node in nodes:
    print(node.text)

Recommended reading:

Sign up to request clarification or add additional context in comments.

4 Comments

Mate I am looking at the tutorial you sent me, they seem to be very nice. I am trying to devise my own way of doing it. Will post soon what I used.
@NikhileshSharma the code snippet that I posted should help you but reading the references is highly recommended :) if you think I helped you in any way, consider upvoting/accepting my answer :)
Mate I was trying something different> i will probably make a new post for it.
@NikhileshSharma didn't findall help you? I demonstrate the usage in the last statement. If you meant really something different, I suggest you to edit you question and I can update my answer :)

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.