I’m trying to read all my QLineEdit fields and the checkBox states and save them to an XML file using Minidom. Below is what I have so far. What is the easiest and shortest way I can write this using a for loop?
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 output:
<?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>
Also, what’s the easiest way to format the xml to look like this?
<?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>
You can try something like this:
The reason I suggested putting the functions as dict values as opposed to their actual text values is because you can use this elements dict as a config object for the xml process at a later stage. Then its just a matter of adding another element to the dict for inclusion.
As for reformatting your xml output, the way its doing it now is how the built in pretty printer works. Text nodes are just another type of child node so it uses a new indented line. You would have to do your own pretty printer function that manually loops over your dom and prints it the way you want (checking for text type nodes and printing them in the same line).
Just in case you aren’t specifically bound to XML, you could shorten this even further using JSON if you wanted:
If order of the elements are important
Using a dictionary will not maintain order of the original entries. If for some reason this is important you can just use a tuple:
Or if using python2.7 (or downloading the backported version for older), you could use an OrderedDict