Solved

Remove Attributes from a XML node string

Posted on 2006-07-04
2
373 Views
Last Modified: 2013-11-19
hey,
if I have a string like so:

<record  spacing="1" padding="1"><trec><thread case="new">Date</thread></trec></record>

its just some xml structure made into a string...
I need a string function that will remove all the attributes from certain nodes:

function StripAttribs(Astring, ATag: string):string;
result string would =

<record><trec><thread case="new">Date</thread></trec></record>

if ATag = 'record' that is
I have made 2 functions myself but I think they are clunky, Im hoping someone might already have something better
0
Comment
Question by:LMuadDIb
2 Comments
 
LVL 28

Accepted Solution

by:
2266180 earned 300 total points
ID: 17040529
this would be a pretty good solution as it is speed and memory optimized (one pass only). Small speed optimizations are still possible.

function StripAttribs(s, t: string):string;
var i:integer; tag:string;
  function getnexttag:boolean;
  begin
    while (i<=length(s)) and (s[i]<>'<') do
      inc(i);
    if s[i]='<' then
    begin
      inc(i);
      tag:='';
      while (i<=length(s)) and (not (s[i] in [' ','>'])) do
      begin
        tag:=tag+s[i];
        inc(i);
      end;
      result:=tag<>'';
    end         else
      result:=false;
  end;
begin
  i:=1;
  t:=lowercase(t);
  while getnexttag do
  begin
    if lowercase(tag)=t then
      while s[i]<>'>' do
        delete(s,i,1);
  end;
  result:=s;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  showmessage(StripAttribs('<record  spacing="1" padding="1"><trec><thread case="new">Date</thread></trec></record>','record'));
end;
0
 
LVL 4

Author Comment

by:LMuadDIb
ID: 17044464
thanks ciuly,
works nice :)
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

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…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

825 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