Solved

Remove Attributes from a XML node string

Posted on 2006-07-04
2
367 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:
ciuly 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
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:
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

744 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