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>