Search code examples
xmldelphidelphi-xe3xliff

How to add new content in a xliff file?


I wanna know how to add a new Line/Element/Content to a Xliff File.

Im use Delphi Xe³ with the TXMLDocument component

<xliff version="1.1">
  <file original="myfile.ext" datatype="plaintext" Source-language="en-US" target-language="fr-FR">
    <body>
      <trans-unit id='#1'>
        <source>Der Text.</source>
        <target>Le texte.</target>
      </trans-unit>
      <trans-unit id='#2'>
        <source>Das Flugzeug</source>
        <target>l'avion.</target>
      </trans-unit>   
      <trans-unit id='#3'>
        <source>Der Baum.</source>
      </trans-unit>  
   </body>
  </file>
</xliff>

I want to insert <target>l'arbre</target> after the <source> line in <trans-unit id='#3'>


Solution

  • const
     c_target='target';
    var
      i:integer;
      NodeElement,NodeElement2: IXMLNode;
    begin
      NodeElement:= XML.ChildNodes.FindNode('xliff');
      if Assigned(NodeElement) then  NodeElement:= NodeElement.ChildNodes.FindNode('file');
      if Assigned(NodeElement) then  NodeElement:= NodeElement.ChildNodes.FindNode('body');
      if Assigned(NodeElement) then
        begin
          for I := 0 to NodeElement.ChildNodes.Count - 1 do
            begin
              if NodeElement.ChildNodes[i].Attributes['id']='#3' then
                begin
                 NodeElement2 := NodeElement.ChildNodes[i].ChildNodes.FindNode(c_target);
                 if not Assigned(NodeElement2) then NodeElement2 := NodeElement.ChildNodes[i].AddChild(c_target);
                 NodeElement2.NodeValue := 'l''arbre';
                 //XML.SaveToFile('C:\temp\test.xml');
                end;
    
            end;
    
        end;
    end;