Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7772271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:51:03+00:00 2026-06-01T16:51:03+00:00

I’m trying to read all my QLineEdit fields and the checkBox states and save

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:51:04+00:00Added an answer on June 1, 2026 at 4:51 pm

    You can try something like this:

    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')
    

    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:

    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()))
    

    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:

    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:
    

    Or if using python2.7 (or downloading the backported version for older), you could use an OrderedDict

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an XML file, the creators of it stuck in a bunch social
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.