Solved

How can I delete a line from a XML?

Posted on 2012-03-13
9
276 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now