Create data in xml from txt file

1k views Asked by At

I want to creat this xml file from a txt file...

i do this Code:

 FXml := TNativeXml.CreateName('Root');
 FXml.XmlFormat := xfReadable;
 open the file
 AssignFile(TFile,'user.txt');
 Reset(TFile);
 while not eof(TFile) do 
 begin
    Readln(TFile,text);
    r :=  Pos(' ',text);
    t2 := Trim(Copy(text,1,Length(text)));
    t1 := Trim(Copy(t2,0,r));
    FXml.Root.NodeNew('row');
    FXml.Root.NodeByName('row').WriteAttributeString('user',t2);
    FXml.Root.NodeByName('row').WriteAttributeString('pin',t1);
 end;
   FXml.SaveToFile('new.xml');
 FXml.free;

something i do wrong with nodebyname but what...

Thank you...

1

There are 1 answers

1
Uwe Raabe On BEST ANSWER

In case your text file contains more than one line, you are creating multiple nodes with the name "row". NodeByName will always return the first node with the given name.

You should store the result of NodeNew in a local variable of type TXmlNode and use that one to set the attributes.

var
  node: TXmlNode
...
node := FXml.Root.NodeNew('row');
node.WriteAttributeString('user',t2);
node.WriteAttributeString('pin',t1);