?
Solved

How can I delete a line from a XML?

Posted on 2012-03-13
9
Medium Priority
?
286 Views
Last Modified: 2012-03-23
Hello Guys

I have a XML that has this line <?xml version="1.0"?>

I need a function that can open the file, delete this line and save the file.

I am attaching a xml with this line

thanks
PO-20483.xml
0
Comment
Question by:hidrau
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 20

Expert Comment

by:edster9999
ID: 37713968
You just read it as a text file and drop line 1.  There are many bits of code to do this already

http://www.google.com/search?q=delphi+edit+text+file

My comment was not how to fix it - but a question.... why ?
If you remove this line then you are making in into an invalid XML file.
Why not just ignore it in the app you are reading it with later instead of editing all the files.
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 37714482
Agree.  WIthout that line most applications won't recognize it or be able to use it as an XML file.

mlmcc
0
 
LVL 1

Author Comment

by:hidrau
ID: 37714627
that line is not necessary in the file.

if I delete it the XML file works fine
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 101

Expert Comment

by:mlmcc
ID: 37714775
I don't have a way to write Pascal code and test it but a basic method would be

Open the file for input - INPUT
Open a file for output - OUTPUT
Read a line from INPUT  -  Skip first line
WHile NOT EOF(INPUT)
   ReadLN INPUT
    WriteLN OUTPUT
Wend

Close INPUT
Close OUTPUT

mlmcc
0
 
LVL 19

Expert Comment

by:Thommy
ID: 37714896
Load file into a StringList, search for line, delete if found and save back to file...

procedure DeleteXMLVersionLine(fname:string);
var
  sl:TstringList;
  idx:integer;
begin
  if FileExists(fname) then begin
    sl:=TStringList.Create;
    sl.LoadFromFile(fname);
    idx:=sl.IndexOf('<?xml version="1.0"?>');
    if idx > -1 then begin
      sl.Delete(idx);
      sl.SaveToFile(fname);
    end;

    sl.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  DeleteXMLVersionLine('c:\swap\test.xml');
end;

Open in new window

0
 
LVL 1

Author Comment

by:hidrau
ID: 37716308
Thommy,

I tested with a xml here and your function didn't find this <?xml version="1.0"?>.

:(
0
 
LVL 101

Accepted Solution

by:
mlmcc earned 2000 total points
ID: 37716627
May have to look for just

xml version="1.0"

or may have to escape the ? since they may be being interpreted as wildcards

'<\?xml version="1.0"\?>'

or even

'<' & chr(asc('?')) & 'xml version="1.0"' & chr(asc('?')) '>'

mlmcc
0
 
LVL 19

Expert Comment

by:Thommy
ID: 37718690
Can you post your XML file for testing...
0
 
LVL 1

Author Closing Comment

by:hidrau
ID: 37758943
thanks very much
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Screencast - Getting to Know the Pipeline
Suggested Courses

749 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