Python parse XML using DOM

Source: Internet
Author: User

Demo

Import Xml.dom.minidom

Dom=xml.dom.minidom.parse (' Sample.xml ')
root = Dom.documentelement
Cc=dom.getelementsbytagname (' movie ')
C1=CC[0]
Print (Root.nodename)
Print (Root.nodevalue)
Print (Root.nodetype)
Print (root. Element_node)

Aa=root.getelementsbytagname (' movie ')
A=AA[0]
Print (' *************************** ')
Print (A.nodename)
Print (A.nodevalue)
Print (A.getattribute (' title '))
Print (A.firstchild)
Print (len (AA))
Print (A.getattribute (' title '))
Print (A.getelementsbytagname (' type ') [0].childnodes[0].data)
Print (a.getelementsbytagname (' format ') [0].childnodes[0].data)

' Parse XML using DOM '
# Coding:utf-8

Import Xml.dom.minidom
From Xml.dom.minidom Import Parse

DT = Xml.dom.minidom.parse (' sample.xml ')
COLLECTION = Dt.documentelement
If Collection.hasattribute (' shelf '):
Print (' Root element:%s '% collection.getattribute (' shelf '))
# Get all films and print detail information
MOVIES = Collection.getelementsbytagname (' movie ')
# Print detailed information for each movie
For movie in MOVIES:
Type_ = Movie.getelementsbytagname (' type ') [0]
Format_ = movie.getelementsbytagname (' format ') [0]
Rating = Movie.getelementsbytagname (' rating ') [0]
Description = Movie.getelementsbytagname (' description ') [0]
Print (' *****movie***** ')
Print (' \ttitle:%s '% movie.getattribute (' title '))
Print (' \ttype:%s '% type_.childnodes[0].data)
Print (' \tformat:%s '% format_.childnodes[0].data)
Print (' \trating:%s '% rating.childnodes[0].data)
Print (' \tdescription:%s '% description.childnodes[0].data)

Python parse XML using DOM

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.