Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 412
  • Last Modified:

Insert contents of a TStringList into another TStringList at a specified index

I've got a situation where I'm creating an XML file for a client.

Currently I've been building the XML inside a TStringList and then saving it to a file using data obtained from a database. This worked fine, but now the client is wanting to 'embed' other XML schemas inside of the main XML schema.

Is it possible to create the main XML in a TStringList and place something like <InsertEmbeddedXMLHere> at the spot where the embedded XML will go and then insert the contents of another TStringList at that position?

Small Example:

TString1                                TString2
----------                                ----------
<XML>                                   <Embedded>
<Tag 1></Tag1>                    <EmTag1></EmTag1>
<Tag2></Tag2>                     <EmTag2></EmTag2>
<InsertXMLHere>                   </Embedded>
<Tag3></Tag3>
</XML>

What I want for results is this:
TString1                                
----------
 <XML>                                
<Tag 1></Tag1>                    
<Tag2></Tag2>                    
<Embedded>
<EmTag1></EmTag1>
<EmTag2></EmTag2>
</Embedded>                
<Tag3></Tag3>
</XML>

Thank you to all that spend time helping with this. I appreciate it.

Jason
0
jasonkrueger
Asked:
jasonkrueger
  • 2
1 Solution
 
ThievingSixCommented:
Here is one way to do it:
procedure TForm1.FormCreate(Sender: TObject);
var
  StringList1,
  StringList2 : TStringList;
  I, J : Integer;
begin
  StringList1 := TStringList.Create;
  StringList2 := TStringList.Create;
  StringList1.Text := '<XML>'#13#10'<Tag 1></Tag1>'#13#10'<Tag2></Tag2>'#13#10'<InsertXMLHere>'#13#10'<Tag3></Tag3>'#13#10'</XML>';
  StringList2.Text := '<Embedded>'#13#10'<EmTag1></EmTag1>'#13#10'<EmTag2></EmTag2>'#13#10'</Embedded>';
  I := StringList1.IndexOf('<InsertXMLHere>');
  If I = -1 Then Exit;
  StringList1.Delete(I);
  For J := StringList2.Count - 1 DownTo 0 Do
    begin
    StringList1.Insert(I,StringList2.Strings[J]);
  end;
  Memo1.Lines := StringList1;
  StringList1.Free;
  StringList2.Free;
end;

Open in new window

0
 
ThievingSixCommented:
And here is a way:
procedure TForm1.FormCreate(Sender: TObject);
var
  StringList1,
  StringList2 : TStringList;
begin
  StringList1 := TStringList.Create;
  StringList2 := TStringList.Create;
  StringList1.Text := '<XML>'#13#10'<Tag 1></Tag1>'#13#10'<Tag2></Tag2>'#13#10'<InsertXMLHere>'#13#10'<Tag3></Tag3>'#13#10'</XML>';
  StringList2.Text := '<Embedded>'#13#10'<EmTag1></EmTag1>'#13#10'<EmTag2></EmTag2>'#13#10'</Embedded>';
  StringList1.Text := StringReplace(StringList1.Text,'<InsertXMLHere>'#13#10,StringList2.Text,[rfReplaceAll]);
  Memo1.Lines := StringList1;
  StringList1.Free;
  StringList2.Free;
end;

Open in new window

0
 
jasonkruegerAuthor Commented:
That's what I was looking for. Thank you!
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now