全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

使用Python生成XML的方法实例

本文实例讲述了使用Python生成XML的方法。分享给大家供大家参考,具体如下:

1. bookstore.py

#encoding:utf-8
'''
根据一个给定的XML Schema,使用DOM树的形式从空白文件生成一个XML。
'''
from xml.dom.minidom import Document
doc = Document() #创建DOM文档对象
bookstore = doc.createElement('bookstore') #创建根元素
bookstore.setAttribute('xmlns:xsi',"http://www.w3.org/2001/XMLSchema-instance")#设置命名空间
bookstore.setAttribute('xsi:noNamespaceSchemaLocation','bookstore.xsd')#引用本地XML Schema
doc.appendChild(bookstore)
############book:Python处理XML之Minidom################
book = doc.createElement('book')
book.setAttribute('genre','XML')
bookstore.appendChild(book)
title = doc.createElement('title')
title_text = doc.createTextNode('Python处理XML之Minidom') #元素内容写入
title.appendChild(title_text)
book.appendChild(title)
author = doc.createElement('author')
book.appendChild(author)
author_first_name = doc.createElement('first-name')
author_last_name = doc.createElement('last-name')
author_first_name_text = doc.createTextNode('张')
author_last_name_text = doc.createTextNode('三')
author.appendChild(author_first_name)
author.appendChild(author_last_name)
author_first_name.appendChild(author_first_name_text)
author_last_name.appendChild(author_last_name_text)
book.appendChild(author)
price = doc.createElement('price')
price_text = doc.createTextNode('28')
price.appendChild(price_text)
book.appendChild(price)
############book1:Python写网站之Django####################
book1 = doc.createElement('book')
book1.setAttribute('genre','Web')
bookstore.appendChild(book1)
title1 = doc.createElement('title')
title_text1 = doc.createTextNode('Python写网站之Django')
title1.appendChild(title_text1)
book1.appendChild(title1)
author1 = doc.createElement('author')
book.appendChild(author1)
author_first_name1 = doc.createElement('first-name')
author_last_name1 = doc.createElement('last-name')
author_first_name_text1 = doc.createTextNode('李')
author_last_name_text1 = doc.createTextNode('四')
author1.appendChild(author_first_name1)
author1.appendChild(author_last_name1)
author_first_name1.appendChild(author_first_name_text1)
author_last_name1.appendChild(author_last_name_text1)
book1.appendChild(author1)
price1 = doc.createElement('price')
price_text1 = doc.createTextNode('40')
price1.appendChild(price_text1)
book1.appendChild(price1)
########### 将DOM对象doc写入文件
f = open('bookstore.xml','w')
f.write(doc.toprettyxml(indent = ''))
f.close()

2. bookstore.xsd

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
 <xsd:element name="bookstore" type="bookstoreType"/>
 <xsd:complexType name="bookstoreType">
  <xsd:sequence maxOccurs="unbounded">
   <xsd:element name="book" type="bookType"/>
  </xsd:sequence>
 </xsd:complexType>
 <xsd:complexType name="bookType">
  <xsd:sequence>
   <xsd:element name="title" type="xsd:string"/>
   <xsd:element name="author" type="authorName"/>
   <xsd:element name="price" type="xsd:decimal"/>
  </xsd:sequence>
  <xsd:attribute name="genre" type="xsd:string"/>
 </xsd:complexType>
 <xsd:complexType name="authorName">
  <xsd:sequence>
   <xsd:element name="first-name" type="xsd:string"/>
   <xsd:element name="last-name" type="xsd:string"/>
  </xsd:sequence>
 </xsd:complexType>
</xsd:schema>

3. 根据上面的XML Schema用Python minidom生成的XML

bookstore.xml

<?xml version="1.0" ?>
<bookstore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="bookstore.xsd">
 <book genre="XML">
  <title>
   Python处理XML之Minidom
  </title>
  <author>
   <first-name>
    张
   </first-name>
   <last-name>
    三
   </last-name>
  </author>
  <price>
   28
  </price>
 </book>
 <book genre="Web">
  <title>
   Python写网站之Django
  </title>
  <author>
   <first-name>
    李
   </first-name>
   <last-name>
    四
   </last-name>
  </author>
  <price>
   40
  </price>
 </book>
</bookstore>

PS:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:

在线XML/JSON互相转换工具:
http://tools./code/xmljson

在线格式化XML/在线压缩XML
http://tools./code/xmlformat

XML在线压缩/格式化工具:
http://tools./code/xml_format_compress

XML代码在线格式化美化工具:
http://tools./code/xmlcodeformat

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python操作xml数据技巧总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。


# Python  # 生成  # XML  # python 生成xml文件  # 以及美化的实例代码  # Python如何生成xml文件  # 利用 Python ElementTree 生成 xml的实例  # 对python 生成拼接xml报文的示例详解  # Python中使用dom模块生成XML文件示例  # python将xml xsl文件生成html文件存储示例讲解  # Python生成xml文件方法示例  # 进阶  # 操作技巧  # 相关内容  # 感兴趣  # 数据结构  # 给大家  # 更多关于  # 所述  # 几款  # 程序设计  # 使用技巧  # 转换工具  # 再为  # 文档  # 编程技巧  # 讲述了  # appendChild  # xsd  # noNamespaceSchemaLocation  # genre 


相关文章: 如何快速上传建站程序避免常见错误?  建站之星各版本价格是多少?  建站之星如何一键生成手机站?  手机钓鱼网站怎么制作视频,怎样拦截钓鱼网站。怎么办?  如何快速查询域名建站关键信息?  如何在景安服务器上快速搭建个人网站?  建站上传速度慢?如何优化加速网站加载效率?  c# 在高并发场景下,委托和接口调用的性能对比  制作农业网站的软件,比较好的农业网站推荐一下?  c++怎么用jemalloc c++替换默认内存分配器【性能】  头像制作网站在线观看,除了站酷,还有哪些比较好的设计网站?  网站制作公司哪里好做,成都网站制作公司哪家做得比较好,更正规?  如何确保FTP站点访问权限与数据传输安全?  如何高效搭建专业期货交易平台网站?  网站制作企业,网站的banner和导航栏是指什么?  制作无缝贴图网站有哪些,3dmax无缝贴图怎么调?  广德云建站网站建设方案与建站流程优化指南  南京做网站制作公司,南京哈发网络有限公司,公司怎么样,做网页美工DIV+CSS待遇怎么样?  建站之星安装模板失败:服务器环境不兼容?  制作网站建设的公司有哪些,网站建设比较好的公司都有哪些?  如何确保西部建站助手FTP传输的安全性?  网站企业制作流程,用什么语言做企业网站比较好?  宝塔建站教程:一键部署配置流程与SEO优化实战指南  如何在万网自助建站中设置域名及备案?  如何选择美橙互联多站合一建站方案?  如何在Tomcat中配置并部署网站项目?  高防网站服务器:DDoS防御与BGP线路的AI智能防护方案  如何在阿里云ECS服务器部署织梦CMS网站?  Python如何创建带属性的XML节点  佛山企业网站制作公司有哪些,沟通100网上服务官网?  相册网站制作软件,图片上的网址怎么复制?  淘宝制作网站有哪些,淘宝网官网主页?  建站之星安装后如何配置SEO及设计样式?  如何选择长沙网站建站模板?H5响应式与品牌定制哪个更优?  正规网站制作公司有哪些,目前国内哪家网页网站制作设计公司比较专业靠谱?口碑好?  实现点击下箭头变上箭头来回切换的两种方法【推荐】  大连企业网站制作公司,大连2025企业社保缴费网上缴费流程?  完全自定义免费建站平台:主题模板在线生成一站式服务  如何选择域名并搭建高效网站?  c# 服务器GC和工作站GC的区别和设置  如何高效配置IIS服务器搭建网站?  潍坊网站制作公司有哪些,潍坊哪家招聘网站好?  定制建站方案优化指南:企业官网开发与建站费用解析  网站制作话术技巧,网站推广做的好怎么话术?  存储型VPS适合搭建中小型网站吗?  Dapper的Execute方法的返回值是什么意思 Dapper Execute返回值详解  娃派WAP自助建站:免费模板+移动优化,快速打造专业网站  沈阳制作网站公司排名,沈阳装饰协会官方网站?  建站上市公司网站建设方案与SEO优化服务定制指南  重庆市网站制作公司,重庆招聘网站哪个好? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。