Python network programming basics-use minidom to generate xml files

Source: Internet
Author: User

1. Use minidom to create an XML file

#
-*-Coding: cp936 -*-
"""
Use minidom to generate xml
1. Create element and createelement
2. Add a subnode and appendchild
3. Create text and createtextnode
4. Create an attribute, createattribute

 res = minidom.Document()
    query = res.createElement("queryItems")
    query.setAttribute('xmlns:xsi','http://www.w3.org/2001/XMLSchema-instance')
    query.setAttribute('xsi:schemaLocation','http://www.taobao.com/schema/personalhomepage queryItems.xsd')
    query.setAttribute('xmlns','http://www.xxx.com/schema/personalhomepage')
    query.setAttribute('xmlns:header','http://www.xxx.com/schema/personalhomepage/extend/headerType')

"""
from xml.dom
import minidom,Node

# Create document
Doc = minidom. Document ()
# Create a book Node
Book = Doc. createelement ("book ")
Doc. appendchild (book)
# Create a title Node
Title = Doc. createelement ("title ")
TEXT = Doc. createtextnode ("sample XML thing ")
Title. appendchild (text)
Book. appendchild (title)
# Creating an author Node
Author = Doc. createelement ("author ")
# Create a Name Node
Name = Doc. createelement ("name ")
First = Doc. createelement ("first ")
First. appendchild (Doc. createtextnode ("Benjamin "))
Name. appendchild (first)

last = doc.createElement("last")
last.appendChild(doc.createTextNode("Smith"))
name.appendChild(last)

Author. appendchild (name)
Book. appendchild (author)
# Author node completed

# Create a chapter Node
Chapter = Doc. createelement ("chapter ")
Chapter. setattribute ("Number", "1 ")
Title = Doc. createelement ("title ")
Title. appendchild (Doc. createtextnode ("Fisrt chapter "))
Chapter. appendchild (title)

para = doc.createElement("para")
para.appendChild(doc.createTextNode("I think widgets are great.you should buy lots \
of them from"))
company = doc.createElement("company")
company.appendChild(doc.createTextNode("Springy widgets,Inc"))
para.appendChild(company)

Chapter. appendchild (para)
# Chapter ter node completion
Book. appendchild (chapter)
# Book node completion

print doc.toprettyxml(indent=
" ")

2. generated XML file

<?xmlversion="1.0"
?>
<book>
    <title>
        Sample XML Thing
    </title>
    <author>
        <name>
            <first>
                Benjamin
            </first>
            <last>
                Smith
            </last>
        </name>
    </author>
    <chapter number="1">
        <title>
            Fisrt Chapter
        </title>
        <para>
            I think widgets are great.you should buy lots of them from
            <company>
                Springy widgets,Inc
            </company>
        </para>
    </chapter>
</book>

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.