Question

I'm working on an import function for certain fields in an XML-based document.

The Reading of the values ​​ansich works perfectly, unfortunately, I can do it does not really attributes of the trans-unit namely read the id.

My code:

procedure TForm2.importXliff;
var
  i: Integer;
  TransUnits: IXMLNodeList;
  ID: IXMLNodeList;
begin
  if OpenDialog.Execute then
  begin
    //XMLDocument erzeugen und xml-Datei laden
    xmlDoc := newXMLDocument;
    xmlDoc.LoadFromFile(OpenDialog.FileName);
    Memo1.Lines := xmlDoc.XML;
    xmlDoc.Active := True;
    TransUnits := xmlDoc.DocumentElement.ChildNodes['file'].ChildNodes['body'].ChildNodes;
    ID := xmlDoc.DocumentElement.ChildNodes['body'].ChildNodes;

      for i := 0 to TransUnits.Count-1 do
        ListBox.Items.Add(TransUnits[i].ChildNodes['source'].Text);
        //xmlDoc.DocumentElement.ChildNodes['trans-unit'].AttributeNodes['id'].Text
  end;
 end;

My xml based text document (Xliff file):

<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>

My question now is how I can read the trans-unit ID?

Was it helpful?

Solution

The IXMLNodeList is a collection of IXMLNode items in whose you can access attributes by name through the Attributes property. In pseudo code it might look like this:

procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
  TransUnits: IXMLNodeList;
  XMLDocument: IXMLDocument;
begin
  XMLDocument := LoadXMLDocument('d:\File.xml');
  if Assigned(XMLDocument) then
  begin
    TransUnits := XMLDocument.DocumentElement.ChildNodes['file'].ChildNodes['body'].ChildNodes;
    for I := 0 to TransUnits.Count - 1 do
      ShowMessage(TransUnits[I].Attributes['id']);
  end;
end;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top