Solved

Remove Attributes from a XML node string

Posted on 2006-07-04
2
379 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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 dynamically set the form action using jQuery.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

690 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