Solved

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

Posted on 2009-04-09
3
407 Views
Last Modified: 2012-05-06
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
Comment
Question by:jasonkrueger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 13

Accepted Solution

by:
ThievingSix earned 500 total points
ID: 24113487
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
 
LVL 13

Expert Comment

by:ThievingSix
ID: 24113490
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
 

Author Closing Comment

by:jasonkrueger
ID: 31568800
That's what I was looking for. Thank you!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month8 days, 4 hours left to enroll

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question