利用 xml.dom.minidom 生成 xml,解决属性无序问题和xml声明单独一行

栏目: 编程语言 · XML · 发布时间: 5年前

内容简介:# cat HKEX-EPS_20180830_003249795.xml<?xml version="1.0" encoding="UTF-8"?

1. 问题描述

属性无序问题和 xml 声明不是单独一行

# cat HKEX-EPS_20180830_003249795.xml

> TCML>< News Encoding="UTF-8" Language="en-us" TimeStamp="20180830194015" >246843820180830194015HKEX-EPSAMENDEDen-usCHANGE OF COMPANY NAME,STOCK SHORT NAME AND COMPANY LOGO20180831MAINfalseHKEX-EPS_20180830_003249795_0.PDFAPPLICATION/PDF521386127001979010000185401400? 地科技股份 满地科技股份 MOODY TECH HLDGET Net IIS Category ListET Net Ltd?2018 ET Net Limited. All rights reserved.

达到效果:

cat HKEX-EPS_20180830_003249795.xml

>

TCML>< News TimeStamp="20180830194015" Encoding="UTF-8" Language="en-us" >246843820180830194015HKEX-EPSAMENDEDen-usCHANGE OF COMPANY NAME,STOCK SHORT NAME AND COMPANY LOGO20180831MAINfalseHKEX-EPS_20180830_003249795_0.PDFAPPLICATION/PDF521386127001979010000185401400? 地科技股份 满地科技股份 MOODY TECH HLDGET Net IIS Category ListET Net Ltd?2018 ET Net Limited. All rights reserved.

2 操作步骤

2.1 环境说明

系统自带 python2.6.6  升级为 python2.7.10

如果没有升级 python2.7

>>> import sys

>>> sys.path

路径为 /usr/lib64/python2.6/xml/dom

使用的模块是

import xml.dom.minidom

2.2 换行处理

# cd /usr/local/lib/python2.7/xml/dom/

原始配置

def writexml(self, writer, indent="", addindent="", newl="",

encoding = None):

if encoding is None:

writer.write(''+ newl )

else:

writer.write('%s' % (encoding, newl ))

for node in self.childNodes:

node.writexml(writer, indent, addindent, newl)

修改配置

def writexml(self, writer, indent="", addindent="", newl="",

encoding = None):

if encoding is None:

writer.write(''+ '\n' )

else:

writer.write('%s' % (encoding, '\n' ))

for node in self.childNodes:

node.writexml(writer, indent, addindent, newl)

2.3 属性有序处理

原始配置

def __init__(self, tagName, namespaceURI=EMPTY_NAMESPACE, prefix=None,

localName=None):

self.tagName = self.nodeName = tagName

self.prefix = prefix

self.namespaceURI = namespaceURI

self.childNodes = NodeList()

self._attrs = {}   # attributes are double-indexed:

self._attrsNS = {} #    tagName -> Attribute

#    URI,localName -> Attribute

# in the future: consider lazy generation

# of attribute objects this is too tricky

# for now because of headaches with

# namespaces.

......

def writexml(self, writer, indent="", addindent="", newl=""):

# indent = current indentation

# addindent = indentation to add to higher levels

# newl = newline string

writer.write(indent+"<" + self.tagName)

attrs = self._get_attributes()

a_names = attrs.keys()

a_names.sort()

修改配置:

def __init__(self, tagName, namespaceURI=EMPTY_NAMESPACE, prefix=None,

localName=None):

self.tagName = self.nodeName = tagName

self.prefix = prefix

self.namespaceURI = namespaceURI

self.childNodes = NodeList()

#self._attrs = {}   # attributes are double-indexed:

self._attrs = OrderedDict()   # attributes are double-indexed:

self._attrsNS = {} #    tagName -> Attribute

#    URI,localName -> Attribute

# in the future: consider lazy generation

# of attribute objects this is too tricky

# for now because of headaches with

# namespaces.

......

def writexml(self, writer, indent="", addindent="", newl=""):

# indent = current indentation

# addindent = indentation to add to higher levels

# newl = newline string

writer.write(indent+"<" + self.tagName)

attrs = self._get_attributes()

a_names = attrs.keys()

#a_names.sort()

3. 总结

亲测可用


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

你凭什么做好互联网

你凭什么做好互联网

曹政 / 中国友谊出版公司 / 2016-12 / 42.00元

为什么有人可以预见商机、超越景气,在不确定环境下表现更出色? 在规则之外,做好互联网,还有哪些关键秘诀? 当环境不给机会,你靠什么翻身? 本书为“互联网百晓生”曹政20多年互联网经验的总结,以严谨的逻辑思维分析个人与企业在互联网发展中的一些错误思想及做法,并给出正确解法。 从技术到商业如何实现,每个发展阶段需要匹配哪些能力、分解哪些目标、落实哪些策略都一一点出,并在......一起来看看 《你凭什么做好互联网》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试