markwalker_
markwalker_

Reputation: 12849

Maintaining special characters when parsing XML with Python?

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? &#10; &#10;The emergency code for the tax year 2011-12 was
        '$previousemergencytaxcode$'. &#10;The emergency code for the tax
        year 2012-13 is '$emergencytaxcode$'. &#10; &#10;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 (&#10;) 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')

Upvotes: 1

Views: 1334

Answers (1)

Michael Anderson
Michael Anderson

Reputation: 73480

I think this is doing exactly what you're telling it to do. The XML contains &#10 which is line break I think. Then you're printing out that string.

If you want to replace the line break with something else in the printed output, then you're probably best doing so after reading it, but before you output it. (Rather than trying to change it in the XML).

Your code will end up looking something like this:

def parse_dialog(self, elem):
    self.out('')
    self.out("item_dialog(id='{0}', text='{1}', type='{2}')".format(
       escape_string(elem.attrib['id']),
       escape_string(elem.attrib['text']),
       escape_string( elem.attrib['type']) ))

def escape_string(s):
  ... 

This is much more robust too as your issue is essentially a script injection issue/vulnerability.

Upvotes: 1

Related Questions