[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Remove Attributes from a XML node string

Posted on 2006-07-04
2
Medium Priority
?
388 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 1200 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
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.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

834 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