Solved

How can I delete a line from a XML?

Posted on 2012-03-13
9
278 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 100

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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 100

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 100

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

773 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