I've got an XML file that I'm parsing with Python & outputting as Python code to a file.
Some of the XML contains Reg Ex values and strings which will be shown as a dialog on screen so there are a few special characters I need to maintain. The code follows, but how can this be done?
The XML looks a bit like this;
<variable id="passportnumber" value="" type="String"> <validate> <regularExpression fieldID="passportnumber" errorID="3007162"><![CDATA[^[a-zA-Z+:?<>;*()%="!0-9./',&\s-]{1,35}$]]></regularExpression> </validate> </variable> And for a dialog;
<if> <condition><![CDATA[$taxcode$ == $previousemergencytaxcode$ and $previousemergencytaxcode$ != $emergencytaxcode$]]></condition> <then> <dialog id="taxCodeOutdatedDialog" text="Are you sure this is the correct tax code? The emergency code for the tax year 2011-12 was '$previousemergencytaxcode$'. The emergency code for the tax year 2012-13 is '$emergencytaxcode$'. Proceed?" type="YES|NO|CANCEL" /> </then> </if> The full Python script is here and the specifics to parse these two are;
def parse_regularExpression(self, elem): self.out('') self.out("item_regularExpression(fieldID='{0}', value='{1}')".format(elem.attrib['fieldID'],elem.text)) def parse_dialog(self, elem): self.out('') self.out("item_dialog(id='{0}', text='{1}', type='{2}')".format(elem.attrib['id'], elem.attrib['text'],elem.attrib['type'])) The line feed ( ) is the main thing I'm unsure how to deal with. It seems that etree is outputting that as a line break even if it is triple quoted. It outputs the text value as;
item_dialog(id='taxCodeOutdatedDialog', text='Are you sure this is the correct tax code? The emergency code for the tax year 2011-12 was '$previousemergencytaxcode$'. The emergency code for the tax year 2012-13 is '$emergencytaxcode$'. Proceed?', type='YES|NO|CANCEL')