Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can try something like this:</p> <pre class="lang-python prettyprint-override"><code>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') </code></pre> <p>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.</p> <p>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).</p> <p>Just in case you aren't specifically bound to XML, you could shorten this even further using JSON if you wanted:</p> <pre class="lang-python prettyprint-override"><code>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())) </code></pre> <h3>If order of the elements are important</h3> <p>Using a dictionary will not maintain order of the original entries. If for some reason this is important you can just use a tuple:</p> <pre><code>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: </code></pre> <p>Or if using python2.7 (or downloading the backported version for older), you could use an <a href="http://docs.python.org/library/collections.html#collections.OrderedDict" rel="nofollow">OrderedDict</a></p>
 

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