Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to save qlineedit fields into an XML file using Python for loops?
    primarykey
    data
    text
    <p>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?</p> <pre class="lang-py prettyprint-override"><code>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 &lt;wol&gt; base element wol = doc.createElement("wol") doc.appendChild(wol) # Create the &lt;mac&gt; node node = doc.createElement("mac") wol.appendChild(node) # Give the &lt;mac&gt; element some text nodeText = doc.createTextNode(mac) node.appendChild(nodeText) # Create the &lt;broadcast&gt; node node = doc.createElement("broadcast") wol.appendChild(node) # Give the &lt;broadcast&gt; element some text nodeText = doc.createTextNode(broadcast) node.appendChild(nodeText) # Create the &lt;broadcast&gt; node node = doc.createElement("destination") wol.appendChild(node) # Give the &lt;broadcast&gt; element some text nodeText = doc.createTextNode(destination) node.appendChild(nodeText) # Create the &lt;port&gt; node node = doc.createElement("port") wol.appendChild(node) # Give the &lt;port&gt; element some text nodeText = doc.createTextNode(port) node.appendChild(nodeText) # Create the &lt;port&gt; node node = doc.createElement("destinationCheckBox") wol.appendChild(node) # Give the &lt;port&gt; element some text nodeText = doc.createTextNode(destinationCheckBox) node.appendChild(nodeText) # Create the &lt;port&gt; node node = doc.createElement("portCheckBox") wol.appendChild(node) # Give the &lt;port&gt; 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 </code></pre> <p>XML output: </p> <pre><code>&lt;?xml version="1.0" ?&gt; &lt;wol&gt; &lt;mac&gt; 00:00:00:00:00:00 &lt;/mac&gt; &lt;broadcast&gt; 192.168.1.255 &lt;/broadcast&gt; &lt;destination&gt; &lt;/destination&gt; &lt;port&gt; 9 &lt;/port&gt; &lt;destinationCheckBox&gt; 0 &lt;/destinationCheckBox&gt; &lt;portCheckBox&gt; 0 &lt;/portCheckBox&gt; &lt;/wol&gt; </code></pre> <p>Also, what's the easiest way to format the xml to look like this? </p> <pre><code>&lt;?xml version="1.0" ?&gt; &lt;wol&gt; &lt;mac&gt;00:00:00:00:00:00&lt;/mac&gt; &lt;broadcast&gt;192.168.1.255&lt;/broadcast&gt; &lt;destination&gt;&lt;/destination&gt; &lt;port&gt;9&lt;/port&gt; &lt;destinationCheckBox&gt;0&lt;/destinationCheckBox&gt; &lt;portCheckBox&gt;0&lt;/portCheckBox&gt; &lt;/wol&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload