如何使用Python for循环将qlineedit字段保存到XML文件中?

时间:2012-04-08 22:01:55

标签: python xml pyqt minidom

我正在尝试读取所有QLineEdit字段和checkBox状态,并使用Minidom将它们保存到XML文件中。以下是我到目前为止的情况。使用for循环可以用最简单最短的方式编写这个函数是什么?

from xml.dom.minidom import Document

# Get all lineEdit values
mac = str(self.lineEdit_mac.text())
broadcast = str(self.lineEdit_broadcast.text())
destination = str(self.lineEdit_destination.text())
port = str(self.lineEdit_port.text())
destinationCheckBox=str(self.checkBox_destination.checkState())
portCheckBox=str(self.checkBox_port.checkState())

# Create the minidom document
doc = Document()

# Create the <wol> base element
wol = doc.createElement("wol")
doc.appendChild(wol)

# Create the <mac> node
node = doc.createElement("mac")
wol.appendChild(node)

# Give the <mac> element some text
nodeText = doc.createTextNode(mac)
node.appendChild(nodeText)

# Create the <broadcast> node
node = doc.createElement("broadcast")
wol.appendChild(node)

# Give the <broadcast> element some text
nodeText = doc.createTextNode(broadcast)
node.appendChild(nodeText)

# Create the <broadcast> node
node = doc.createElement("destination")
wol.appendChild(node)

# Give the <broadcast> element some text
nodeText = doc.createTextNode(destination)
node.appendChild(nodeText)

# Create the <port> node
node = doc.createElement("port")
wol.appendChild(node)

# Give the <port> element some text
nodeText = doc.createTextNode(port)
node.appendChild(nodeText)

# Create the <port> node
node = doc.createElement("destinationCheckBox")
wol.appendChild(node)

# Give the <port> element some text
nodeText = doc.createTextNode(destinationCheckBox)
node.appendChild(nodeText)

# Create the <port> node
node = doc.createElement("portCheckBox")
wol.appendChild(node)

# Give the <port> element some text
nodeText = doc.createTextNode(portCheckBox)
node.appendChild(nodeText)

# Write to document
f = open(fileName, 'w')
doc.writexml(f, indent='',addindent='  ',newl='\n')
f.closed

XML输出:

<?xml version="1.0" ?>
<wol>
  <mac>
    00:00:00:00:00:00
  </mac>
  <broadcast>
    192.168.1.255
  </broadcast>
  <destination>

  </destination>
  <port>
    9
  </port>
  <destinationCheckBox>
    0
  </destinationCheckBox>
  <portCheckBox>
    0
  </portCheckBox>
</wol>

另外,将xml格式化为最简单的方法是什么?

<?xml version="1.0" ?>
<wol>
  <mac>00:00:00:00:00:00</mac>
  <broadcast>192.168.1.255</broadcast>
  <destination></destination>
  <port>9</port>
  <destinationCheckBox>0</destinationCheckBox>
  <portCheckBox>0</portCheckBox>
</wol>

1 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情:

from xml.dom.minidom import Document

# Get all lineEdit values
elements = dict(
    mac = self.lineEdit_mac.text,
    broadcast = self.lineEdit_broadcast.text,
    destination = self.lineEdit_destination.text,
    port = self.lineEdit_port.text,
    destinationCheckBox = self.checkBox_destination.checkState,
    portCheckBox = self.checkBox_port.checkState
)

doc = Document()
wol = doc.createElement("wol")
doc.appendChild(wol)

for name, fn in elements.iteritems():
    node = doc.createElement(name)
    wol.appendChild(node)

    text = str(fn())
    nodeText = doc.createTextNode(text)
    node.appendChild(nodeText)

with open(fileName, 'w') as f:
    doc.writexml(f, indent='', addindent='  ', newl='\n')

我建议将函数作为dict值而不是实际文本值的原因是因为您可以在稍后阶段将此元素dict用作xml进程的配置对象。然后它只是在dict中添加另一个元素以包含它。

至于重新格式化你的xml输出,它现在的方式是内置漂亮的打印机的工作方式。文本节点只是另一种子节点,因此它使用新的缩进行。你必须做你自己漂亮的打印机功能,手动循环你的dom并按照你想要的方式打印它(检查文本类型节点并在同一行打印它们)。

如果您没有专门绑定XML,如果您愿意,可以使用JSON进一步缩短它:

import json

elements = dict(
    mac = self.lineEdit_mac.text,
    broadcast = self.lineEdit_broadcast.text,
    destination = self.lineEdit_destination.text,
    port = self.lineEdit_port.text,
    destinationCheckBox = self.checkBox_destination.checkState,
    portCheckBox = self.checkBox_port.checkState
)

with open(fileName, 'w') as f:
    # if you need the root key
    data = {'wol': dict((name, str(fn())) for name, fn in elements.iteritems())}
    json.dump(data, f, indent=4)

    # or, just the key/values
    #json.dump(elements, f, indent=4, default=lambda o: str(o()))

如果要素的顺序很重要

使用字典不会维护原始条目的顺序。如果由于某种原因这很重要,你可以使用一个元组:

elements = (
    ('mac', self.lineEdit_mac.text),
    ('broadcast', self.lineEdit_broadcast.text),
    ('destination', self.lineEdit_destination.text),
    ('port', self.lineEdit_port.text),
    ('destinationCheckBox', self.checkBox_destination.checkState),
    ('portCheckBox', self.checkBox_port.checkState)
)

# and remove .iteritems() where previously used
for name, fn in elements:

或者如果使用python2.7(或下载旧版的后移版本),您可以使用OrderedDict