0

I am working on a short project in python that looks something like this:

def UserInput():
    inputFuel = TextCheck('Oil','Gas')
    inputLiters = NumberCheck('How many liters did you fill: ')
    inputMoney = NumberCheck('How much did you pay: ')
    inputGasStation = str(input('On what gas station were you: '))
    inputDate = Datecheck = ('Input date: ')

    MakeXml(InputFuel, InputLiters, InputMoney, inputGasStation, inputDate)

def MakeXml(fuel, liters, money, gasStation, date):
    elementInputs = Element('Inputs')
    elementTree = ElementTree(elementInputs)

    elementInput = Element('Input')
    elementInputs.append(elementInput)

    elementFuel = Element('Fuel')
    elementFuel.text = fuel
    elementInput.append(elementFuel)

    elementLiters = Element('Liters')
    elementLiters.text = liters
    elementInput.append(elementLiters)

    elementMoney = Element('Money')
    elementMoney.text = money
    elementInput.append(elementMoney)

    elementGasStation = Element('Gas station')
    elementGasStation.text = gasStation
    elementInput.append(elementBenzinska)

    elementDate = Element('Date')
    elementDate.text = date
    elementInput.append(elementDate)

    elementTree.write('inputs.xml')
    print('Input saved!')

and my problem is that when i type something in i get only that input saved in the XML and if i try to type something else the input before that gets replaced by the new one. I need the program to work so the every input gets saved in the Input element and not replaced.(I translated the program from my language to English so I apologize if i forgot to translate something or typed something that doesn't make sense.

  • Several things here: `Element()` function needs to be prefixed by the imported `lxml.etree` module. Also, you need to define a root node of document to contain all Inputs and children. Please post all relevant parts of code, not just the snippet of problem source. You could even reduce lines with [SubElement](http://lxml.de/tutorial.html) where node is created and appended with text value. – Parfait Sep 14 '15 at 01:11

3 Answers3

0

Use this when opening the file:

with open("test.txt", "a") as myfile:
    myfile.write("appended text")

Source: How do you append to a file?

Community
  • 1
  • 1
Bazinga
  • 489
  • 1
  • 5
  • 16
0

Simply output XML in a different function and call it after all inputs. Have the function pass element's root node (containing all nodes) to a string that is then written to file:

def OutputXML(root):
     # PASS XML CONTENT TO STRING
     tree_out = (ElementTree.tostring(root, pretty_print=True, xml_declaration=True, encoding="UTF-8"))

     # OPEN XML FILE
     xmlfile = open('inputs.xml','wb') 

     # WRITE TO XML FILE
     xmlfile.write(tree_out)
     print('Input saved!')
Parfait
  • 104,375
  • 17
  • 94
  • 125
0

try open the file as read, and write with elementTree.write function

import xml.etree.ElementTree as ET
from xml.etree.ElementTree import Element

elementInputs = Element('Inputs')
elementTree = ET.ElementTree(elementInputs)
try:
    with open('inputs.xml', 'rw') as f:
        elementTree.parse(f)
except Exception as e:
    print(e)

elementInputs = elementTree.getroot()

elementInput = Element('Input')
elementFuel = Element('Fuel')
...

elementInputs.append(elementInput)

elementTree.write('inputs.xml')

also "Gas station" should be GasStation or similar for a valid token

mvaldes
  • 11
  • 4