首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用toprettyxml()在同一行中给xml标记和文本

如何使用toprettyxml()在同一行中给xml标记和文本
EN

Stack Overflow用户
提问于 2015-07-31 20:13:25
回答 1查看 1.2K关注 0票数 1

我有这个文本文件20150731100543_1.txt

代码语言:javascript
复制
GI-eSTB-MIB-NPH::eSTBGeneralErrorCode.0 = INTEGER: 0
GI-eSTB-MIB-NPH::eSTBGeneralConnectedState.0 = INTEGER: true(1)
GI-eSTB-MIB-NPH::eSTBGeneralPlatformID.0 = INTEGER: 2075
GI-eSTB-MIB-NPH::eSTBMoCAfrequency.0 = INTEGER: 0
GI-eSTB-MIB-NPH::eSTBMoCAMACAddress.0 = STRING: 0:0:0:0:0:0
GI-eSTB-MIB-NPH::eSTBMoCANumberOfNodes.0 = INTEGER: 0

我想像下面这样用xml进行转换(20150731100543_1.xml)

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<doc>
    <GI-eSTB-MIB-NPH>
        <eSTBGeneralErrorCode.0>
            INTEGER: 0
        </eSTBGeneralErrorCode.0>
    </GI-eSTB-MIB-NPH>
    <GI-eSTB-MIB-NPH>
        <eSTBGeneralConnectedState.0>
            INTEGER: true(1)
        </eSTBGeneralConnectedState.0>
    </GI-eSTB-MIB-NPH>
    <GI-eSTB-MIB-NPH>
        <eSTBGeneralPlatformID.0>
            INTEGER: 2075
        </eSTBGeneralPlatformID.0>
    </GI-eSTB-MIB-NPH>
    <GI-eSTB-MIB-NPH>
        <eSTBMoCAfrequency.0>
            INTEGER: 0
        </eSTBMoCAfrequency.0>
    </GI-eSTB-MIB-NPH>
    <GI-eSTB-MIB-NPH>
        <eSTBMoCAMACAddress.0>
            STRING: 0:0:0:0:0:0
        </eSTBMoCAMACAddress.0>
    </GI-eSTB-MIB-NPH>
    <GI-eSTB-MIB-NPH>
        <eSTBMoCANumberOfNodes.0>
            INTEGER: 0
        </eSTBMoCANumberOfNodes.0>
    </GI-eSTB-MIB-NPH>
</doc>

我可以使用以下脚本完成这一任务:

代码语言:javascript
复制
import sys
import time
import commands
from xml.etree.ElementTree import Element, SubElement
from xml.etree import ElementTree
from xml.dom import minidom

def prettify(elem):
    """Return a pretty-printed XML string for the Element.
    """
    rough_string = ElementTree.tostring(elem, 'utf-8')
    reparsed = minidom.parseString(rough_string)
    return reparsed.toprettyxml(indent="    ", newl="\n", encoding="UTF-8")

if len(sys.argv) != 2:
    print "\nUsage: python script.py <IP>\n";
    exit(0)
filename_xml = '20150731100543_1.xml'#filename_xml = temp + ".xml"
print "xml filename is: %s\n" % filename_xml
xml = open(filename_xml, 'w+')

top = Element('doc')

with open('20150731100543_1.txt') as f:
    for line in f:
        b = line.split(':')
        child = SubElement(top, b[0])

        c = line.split()
        d = c[0].split(':')
        property =  SubElement(child, d[2])

        property.text = c[2] + " " + c[3]

xml.write(prettify(top))

xml.close()

我有三个问题:

  1. 是否有任何方法(使用toprettyxml()或其他什么)来更改正在生成的xml,使其在同一行的标记中具有开放和结束标记和文本?
  2. 另外,我可以只在结尾处开始时使用标记,而不是在它下面的每个元素中都有标记吗?(因为所有元素都在这个标记中)

因此,如果可能的话,xml的格式应该是:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<doc>
    <GI-eSTB-MIB-NPH>
        <eSTBGeneralErrorCode.0>INTEGER: 0</eSTBGeneralErrorCode.0>
        <eSTBGeneralConnectedState.0>INTEGER: true(1)</eSTBGeneralConnectedState.0>
        <eSTBGeneralPlatformID.0>INTEGER: 2075</eSTBGeneralPlatformID.0>
        <eSTBMoCAfrequency.0>INTEGER: 0</eSTBMoCAfrequency.0>
        <eSTBMoCAMACAddress.0>STRING: 0:0:0:0:0:0</eSTBMoCAMACAddress.0>
        <eSTBMoCANumberOfNodes.0>INTEGER: 0</eSTBMoCANumberOfNodes.0>
    </GI-eSTB-MIB-NPH>
</doc>

我正在尝试这样做,因为这将在很大程度上减少xml中的行数。

最后一个也是最不重要的问题是:

  1. 是否有更好的方法来获取每一行的子字符串,而不是使用split() 以open('20150731100543_1.txt')作为f: for行,在f: B= line.split(':')子=SubElement(顶部,b) C= line.split() d= c.split(':')属性=SubElement(子,d2) property.text = c2 +“”+ c3

请原谅我写了这么长的文章。

EN

回答 1

Stack Overflow用户

发布于 2016-04-01 20:39:52

1& 2:我使用etree.tostring,没有任何这些问题。

3:可以用regex替换多个拆分操作。

这样做应该很好:

代码语言:javascript
复制
from lxml import etree
import re

filename_xml = '20150731100543_1.xml'

root = etree.Element('doc')
node = etree.SubElement(root, 'GI-eSTB-MIB-NPH')
f = open('20150731100543_1.txt')
text = f.read()
f.close()

# get tag and value from each row
for tag, value in re.findall('GI-eSTB-MIB-NPH::(.*) = (.*$)', text, re.MULTILINE):
   # create child node
   etree.SubElement(node, tag).text = value

xml = etree.tostring(root, pretty_print = True, encoding = 'utf-8', xml_declaration=True)

f = open(filename_xml, 'w')
f.write(xml)
f.close
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31754461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档