I am trying to replace certain fields of an xml file in python, the xml file looks like this:
      ...
      <DialogEntry ID="179" IsRoot="false" IsGroup="false" NodeColor="Pink" DelaySimStatus="false" FalseCondtionAction="Block" ConditionPriority="Normal">
        <Fields>
          <Field Hint="(Wird Ingame nicht verwendet.)" Type="Text">
            <Title>Title</Title>
            <Value>Reaktion TS34 Antwort 2</Value>
          </Field>
          <Field Hint="The actor who is talking." Type="Actor">
            <Title>Actor</Title>
            <Value>2</Value>
          </Field>
          <Field Hint="The actor who is listening." Type="Actor">
            <Title>Conversant</Title>
            <Value>1</Value>
          </Field>
          <Field Hint="The text that is spoken by the actor." Type="Localization">
            <Title>Dialogue Text</Title>
            <Value>[Speaking]</Value>
          <Field Hint="Audiofile to play" Type="Text">
            <Title>Audio-File</Title>
            <Value />
          </Field>
        </Fields>
        <ReviewerNotes />
        <ReviewerStatus>None</ReviewerStatus>
        <OutgoingLinks />
        <ConditionsString />
        <UserScript />
      </DialogEntry>
    </DialogEntries>
Please note that the xml file consists of multiple DialogEntries and there are more Fields than the ones shown but what I want to do is: For a certain DialogEntry ID for example 179 I want to replace: <Title>Audio-File</Title>...<Value /> with generic text like <Title>Audio-File</Title>...<Value>Audiofile_XYZ.mp3</Value>
I have been trying with regular expressions, somethingl like this:
  r1 = re.compile("<DialogEntry ID=\"%d\".*?<Title>Audio-File</Title>\n {16}<Value />" % (id_to change),re.DOTALL)
  r2 = re.compile("<DialogEntry ID=\"%d\".*?<Title>Audio-File</Title>\n {16}<Value>%s</Value>"  % (id_to change, filename), re.DOTALL)
  content = re.sub(r1,r2 ,content)
but I am stuck because it's not working as expected. The problems I have are:
- Making the RE match multiple lines(re.DOTALL seams only to work with precompiled REs for re.sub())
- The indicator ID and the part to replace are quite far away and there is a lot of dynamic text inbetween, how can I identify the correct DialogueEntry and still replace only the part I want to change without having to deal with the Titles between Audio-File and ID
Can you please help me out or show me a more appropritate way to do these changes?
Regards, BPR
 
    