Python xml file operation example

Source: Internet
Author: User
This article describes how to operate xml files in python. For more information, see The code is as follows:


Def get_seed_data (filename ):
Dom = minidom. parse (filename)
Root = dom.doc umentElement
System_nodes = root. getElementsByTagName ("system ")
K = 0
Seed_list = []
For system_node in system_nodes:
# Print system_node.nodeName + 'Id = '+ system_node.getAttribute ('id ')
System_id = system_node.getAttribute ("id ")
System_name = system_node.getAttribute ("name ")
# Print 'system _ name: % s' % system_name
Section_nodes = system_node.getElementsByTagName ("section ")
For section_node in section_nodes:
Section_id = section_node.getAttribute ('id ')
Section_name = section_node.getAttribute ('name ')
# Print ''+ section_node.nodeName + 'Id = '+ section_id + 'name =' + section_name
Crawl_cycle_node = section_node.getElementsByTagName ("crawl_cycle ")
Crawl_cycle = crawl_cycle_node [0]. childNodes [0]. nodeValue
# Print ''+ crawl_cycle_node [0]. nodeName + '=' + crawl_cycle
Seed_nodes = section_node.getElementsByTagName ('seed ')
For seed_node in seed_nodes:
Seed = {}
Seed ['crawl _ cycle'] = crawl_cycle
Seed ['system _ id'] = int (system_id)
Seed ['system _ name'] = system_name
Seed ['Section _ id'] = int (section_id)
Seed ['Section _ name'] = section_name
Seed_id = seed_node.getAttribute ('id ')
Seed ['seed _ id'] = int (seed_id)
# Print ''+ seed_node.nodeName +'' + 'Id = '+ seed_id
Userblog_url_node = seed_node.getElementsByTagName ('userblog _ url ')
Userblog_url = userblog_url_node [0]. childNodes [0]. nodeValue
Seed ['userblog _ url'] = userblog_url
# Print ''+ 'userblog _ url' +'' + userblog_url
Print '-------------------------------------------'
Print 'system _ id: % d' % seed ['system _ id']
Print 'system _ name: % s' % seed ['system _ name']
Print 'section_id: % d' % seed ['Section _ id']
Print 'section_name: % s' % seed ['Section _ name']
Print 'seed_id: % d' % seed ['seed _ id']
Print 'userblog_url: % s' % seed ['userblog _ url']
Print '='
Seed_list.append (seed)
Print seed_list [k]
K + = 1
OS. system ('pause ')
Return seed_list

The code is as follows:








Http://aaa.com.cn/loveissuuny </userblog_url>


Http://aaa.com.cn/loveissuuny </userblog_url>


Http://aaa.com.cn/sanxiazaixian </userblog_url>





Http://aaa.com.cn/twocold </userblog_url>


Http://aaa.com.cn/u/1233526741 </userblog_url>



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.