简体   繁体   English

替换Python中的XML字段

[英]Replace XML field in Python

I am trying to replace certain fields of an xml file in python, the xml file looks like this: 我正在尝试在python中替换xml文件的某些字段,该xml文件如下所示:

      ...
      <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> 请注意,该xml 文件由多个DialogEntries组成,并且比显示的字段更多,但我要执行的操作是:对于某个179的DialogEntry ID, 我要替换: <Title>Audio-File</Title>...<Value />带有诸如<Title>Audio-File</Title>...<Value>Audiofile_XYZ.mp3</Value>这样的通用文本<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()) 使RE匹配多行(re.DOTALL接缝仅与re.sub()的预编译RE一起使用)
  • 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 指示器ID和要替换的部分相距很远,并且它们之间有很多动态文本,如何识别正确的DialogueEntry并仍然仅替换我要更改的部分,而不必处理音频文件之间的标题和ID

Can you please help me out or show me a more appropritate way to do these changes? 您能否请我帮忙,或向我展示更适当的方式进行这些更改?

Regards, BPR 此致BPR

You should probably consider a more appropriate medium for your transformations: XML -> text -> XML is doomed to failure / unnecessary complexity for non-trivial problems. 您可能应该考虑一种更合适的转换方式:XML->文本-> XML注定要失败/非平凡问题的不必要复杂性。

Perhaps it'd be best to parse the file using the ElementTree XML API (consider using lxml if performance is important). 也许最好使用ElementTree XML API解析文件(如果性能很重要,请考虑使用lxml )。 Then you can modify the XML representation for your <Value/> nodes in the tree using the API, and then write to an output file . 然后,您可以使用API 修改树中<Value/>节点的XML表示形式 ,然后将其写入输出文件

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; 另外,在Python之外(完全或几乎完全取决于您如何调用它),您甚至可以使用XSLT将此XML转换为稍有不同的XML。 that's what it was designed for after all. 毕竟这是它的设计目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM