Solved

How can I delete a line from a XML?

Posted on 2012-03-13
9
282 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
[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
  • 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
Industry Leaders: 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 500 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

Industry Leaders: 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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

705 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