0

我正在尝试读取我所有的 QLineEdit 字段和复选框状态,并使用 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>
4

1 回答 1

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 进程的配置对象。然后它只是向字典添加另一个元素以供包含的问题。

至于重新格式化您的 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

于 2012-04-08T22:35:04.317 回答