0

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

2

1 Answer 1

1

You should probably consider a more appropriate medium for your transformations: XML -> text -> XML is doomed to failure / unnecessary complexity for non-trivial problems.

Perhaps it'd be best to parse the file using the ElementTree XML API (consider using lxml if performance is important). Then you can modify the XML representation for your <Value/> nodes in the tree using the API, and then write to an output file.

Alternatively, outside of Python completely (or almost completely, depending how you invoke it), you could even just use XSLT to transform this XML to slightly different XML; that's what it was designed for after all.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.