1

I have a method which saves from reservations, but overwrites the output file every time when I create a new instance of XmlReportGenerator

from lxml import etree
from Flights.FlightTravelReservation import FlightTravelReservation

class XmlReportGenerator:
    root = None
    doc = None

    def __init__(self):
        self.root = etree.Element('results')
        self.doc = etree.ElementTree(self.root)

    def add_flight_row(self, flight_travel_reservation):
        page_element = etree.SubElement(self.root, 'FlightTravel')
        etree.SubElement(page_element, 'QuantityOfPassengers').text = 
             str(len(flight_travel_reservation.paxes))
        etree.SubElement(page_element,'Id').text = 
             flight_travel_reservation.reservation_id

    def Save(self, path = 'Flights.xml'):
        outFile = open(path, 'wb')
        self.doc.write(outFile)

Where sample output is:

<results>
  <FlightTravel>
    <Paxes>4</Paxes>
    <Id>259183</Id>
  </FlightTravel>
</results>

How do I modify add_flight_row method to add new rows? For example, I want something like:

<results>
  <FlightTravel>
    <Paxes>4</Paxes>
    <Id>259183</Id>
  </FlightTravel>
  <FlightTravel>
    <Paxes>9</Paxes>
    <Id>123456</Id>
  </FlightTravel>
</results>

I added test method:

def test_test(self,a,b):

    page_element = etree.SubElement(self.root,'FlightTravel')

    etree.SubElement(page_element,'QuantityOfPassengers').text = a
    etree.SubElement(page_element,'Id').text = b

    return page_element

And this is how I use it:

x=XmlReportGenerator()
x.test_test("a","b")
x.Save("test.xml")

x=XmlReportGenerator()
x.test_test("c","d")
x.Save("test.xml")

and in result I have:

<results>
  <FlightTravel>
    <Flights/>
    <QuantityOfPassengers>c</QuantityOfPassengers>
    <Id>d</Id>
  </FlightTravel>
</results>

instead of

<results>
  <FlightTravel>
    <Flights/>
    <QuantityOfPassengers>a</QuantityOfPassengers>
    <Id>b</Id>
  </FlightTravel>
  <FlightTravel>
    <Flights/>
    <QuantityOfPassengers>c</QuantityOfPassengers>
    <Id>d</Id>
  </FlightTravel>
</results>
4
  • You have not well-formed XML in both of your examples. Commented May 30, 2011 at 9:58
  • No, I can open it using IE so it's ok. There is a node flighs, but don't look at it. Now it is not important :) Commented May 30, 2011 at 10:11
  • Since you don't show code that calls add_flight_row I guess that you are Saveing every results tree for each element of flight_travel_reservation or are not looping at all. The code doesn't match the output, either, so please paste the actual code and output. Commented May 30, 2011 at 11:07
  • +1 thanks for clarifying the question. Every XML document has one and only one root. Therefore, if Save must generate a well-formed document it cannot simply concatenate today's <results></results> onto yesterday's. Thus phihag's answer. Commented May 31, 2011 at 11:51

2 Answers 2

2

You'll need to define a Load function for XmlReportGenerator to load the old results.

class XmlReportGenerator(object):
    def __init__(self):
    # __init__, add_flight_row, and Save

    def load(self, path='Flights.xml'):
        try:
            self.doc = etree.ElementTree()
            self.root = self.doc.parse(path)
        except IOError:
            pass

and use it like this:

# day 1
x=XmlReportGenerator()
x.load('test.xml') # Silently catches the error if the file does not exit yet
x.test_test("a","b")
x.Save('test.xml')

# day2
x=XmlReportGenerator()
x.load('test.xml')
x.test_test("c","d")
x.Save("test.xml")
Sign up to request clarification or add additional context in comments.

2 Comments

So I can't? I will be running this code everyday, so there is no chance to use the same instance of XmlReportGenerator
@user278618 I'm sorry, I didn't realize you wanted to append cd to the ab file, not just create the file with abcd.
0

Just call add_flight_row (or test_test) repeatedly on the same instance for each row you want to add:

x = XmlReportGenerator()
x.test_test("a", "b")
x.test_test("c", "d")
x.Save("test.xml")

1 Comment

So I can't? I will be running this code everyday, so there is no chance to use the same instance of XmlReportGenerator

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.