Board index » Visual Studio » String Manipulation Question - Stripping XML Tags

String Manipulation Question - Stripping XML Tags

Visual Studio225
I am looking for a simple piece of code for stripping the XML tags from

a string. Here is a sample string:



<Top>mystring</Top>



I want te remove the tags and be left with this:



mystring



What is an easy way to do this using some sort of string manipulation?



Thanks,

John


-
 

Re:String Manipulation Question - Stripping XML Tags

jcrouse1@hotmail.com's wild thoughts were released on 1 Aug

2006 07:36:45 -0700 bearing the following fruit:



Quote
I am looking for a simple piece of code for stripping the XML tags from

a string. Here is a sample string:



<Top>mystring</Top>



I want te remove the tags and be left with this:



mystring



What is an easy way to do this using some sort of string manipulation?



Thanks,

John



Replace()









Jan Hyde (VB MVP)



--

"Desert Crossing" by Rhoda Camel (Gnu Bee)



-

Re:String Manipulation Question - Stripping XML Tags

Í'd probably simply index for the first ">" and then index from there to the

first "<" and substring (mid) the chunk in between.



(If you might have multiple tags ahead of the target, then check if the

character following the first ">" is "<". If so, then index thereafter for

the next ">", and so on until you find a ">" without a following "<".)



Nigel





<jcrouse1@hotmail.com>wrote in message

Quote
I am looking for a simple piece of code for stripping the XML tags from

a string. Here is a sample string:



<Top>mystring</Top>



I want te remove the tags and be left with this:



mystring



What is an easy way to do this using some sort of string manipulation?



Thanks,

John







-

Re:String Manipulation Question - Stripping XML Tags

That would work, but I'd do it slightly differently, looking for a '<'=20

and then a '>', then remove them (and everything between, of course). =20

Repeat in a loop until no more pairs of <>found. What is left is your=20

data. This of course assumes that you will never have a '<' as part of=20

the data. If you do, then your parsing becomes more complex.







In article <44cf6d35$1_2@mk-nntp-2.news.uk.tiscali.com>,=20

nigel@bufton.org says...

Quote
=CD'd probably simply index for the first ">" and then index from there t=

o the=20

first "<" and substring (mid) the chunk in between.

=20

(If you might have multiple tags ahead of the target, then check if the=

=20

character following the first ">" is "<". If so, then index thereafter f=

or=20

the next ">", and so on until you find a ">" without a following "<".)

=20

Nigel

=20

=20

<jcrouse1@hotmail.com>wrote in message=20

news:1154443004.954881.37360@i3g2000cwc.googlegroups.com...

>I am looking for a simple piece of code for stripping the XML tags from

>a string. Here is a sample string:

>

><Top>mystring</Top>

>

>I want te remove the tags and be left with this:

>

>mystring

>

>What is an easy way to do this using some sort of string manipulation?

>

>Thanks,

>John

>=20

=20

=20

=20



--=20

Remove the ns_ from if replying by e-mail (but keep posts in the=20

newsgroups if possible).

-

Re:String Manipulation Question - Stripping XML Tags

Nigel,

I like your idea. got an example? There will never be multiple tags

in the string.

John



Nigel Bufton wrote:

Quote
=CD'd probably simply index for the first ">" and then index from there t=

o the

first "<" and substring (mid) the chunk in between.



(If you might have multiple tags ahead of the target, then check if the

character following the first ">" is "<". If so, then index thereafter f=

or

the next ">", and so on until you find a ">" without a following "<".)



Nigel





<jcrouse1@hotmail.com>wrote in message

news:1154443004.954881.37360@i3g2000cwc.googlegroups.com...

>I am looking for a simple piece of code for stripping the XML tags from

>a string. Here is a sample string:

>

><Top>mystring</Top>

>

>I want te remove the tags and be left with this:

>

>mystring

>

>What is an easy way to do this using some sort of string manipulation?

>

>Thanks,

>John

>



-

Re:String Manipulation Question - Stripping XML Tags

Why not use an XML Parser? MSXML?



<jcrouse1@hotmail.com>wrote in message

I am looking for a simple piece of code for stripping the XML tags from

a string. Here is a sample string:



<Top>mystring</Top>



I want te remove the tags and be left with this:



mystring



What is an easy way to do this using some sort of string manipulation?



Thanks,

John





-

Re:String Manipulation Question - Stripping XML Tags

Quote
I am looking for a simple piece of code for stripping the XML

tags from a string. Here is a sample string:



<Top>mystring</Top>



I want te remove the tags and be left with this:



Show us a more complete example string that you will have to deal with so we

will know what have and what you need from it.



Rick





-

Re:String Manipulation Question - Stripping XML Tags

Rick,

Actually that is as bad as it gets. I completely control the XML

file since I am writing it myself. Simply an opening tag, a closing tag

and the string I want in between. As bad as the string gets is a

possible "-" (minus) and that's it.



John



Rick Rothstein wrote:

Quote
>I am looking for a simple piece of code for stripping the XML

>tags from a string. Here is a sample string:

>

><Top>mystring</Top>

>

>I want te remove the tags and be left with this:



Show us a more complete example string that you will have to deal with so we

will know what have and what you need from it.



Rick



-

Re:String Manipulation Question - Stripping XML Tags

Quote
>>I am looking for a simple piece of code for stripping the XML

>>tags from a string. Here is a sample string:

>>

>><Top>mystring</Top>

>>

>>I want te remove the tags and be left with this:

>

>Show us a more complete example string that you will have to deal with so

>we

>will know what have and what you need from it.



Actually that is as bad as it gets. I completely control the XML

file since I am writing it myself. Simply an opening tag, a closing tag

and the string I want in between.



Probably not the fastest method, but one of the more simple to write...



XMLstring = "<Top>mystring</Top>"

Data = Split(Split(XMLstring, ">")(1), "<")(0)





Quote
As bad as the string gets is a possible "-" (minus) and that's it.



I'm not sure what this line meant, so I'm not sure if the code above covers

it or not.





Rick





-

Re:String Manipulation Question - Stripping XML Tags

You could do this using regular expressions. This link gives an example of

using

regular expressions to strip HTML tags, be easy to strip XML with it too.



www.4guysfromrolla.com/webtech/042501-1.shtml">www.4guysfromrolla.com/webtech/042501-1.shtml



<jcrouse1@hotmail.com>wrote in message

Quote
I am looking for a simple piece of code for stripping the XML tags from

a string. Here is a sample string:



<Top>mystring</Top>



I want te remove the tags and be left with this:



mystring



What is an easy way to do this using some sort of string manipulation?



Thanks,

John







-

Re:String Manipulation Question - Stripping XML Tags

"David Kerber" <ns_dkerber@ns_WarrenRogersAssociates.com>wrote in message



Quote
This of course assumes that you will never have a '<' as part of

the data.



Which, assuming well-formed XML, would only happen inside a CDATA section.





-

Re:String Manipulation Question - Stripping XML Tags

<jcrouse1@hotmail.com>wrote in message



Quote
Actually that is as bad as it gets. I completely control the XML

file since I am writing it myself.



If you're in complete control then why are you building something and then

tearing it apart? Just keep the pieces separate, combining them when

necessary and using the individual pieces when necessary.





-

Re:String Manipulation Question - Stripping XML Tags

John:



If you are just looking for data between the first ">" and the following

"<", then this will do it:



i1 = Instr(xmlstring, ">")

i2 = Instr(i1, xmlstring, "<")

datapart = Mid(xmlstring, i1 + 1, i2 - i1 - 1)



Nigel



<jcrouse1@hotmail.com>wrote in message

Nigel,

I like your idea. got an example? There will never be multiple tags

in the string.

John



Nigel Bufton wrote:

Quote
Í'd probably simply index for the first ">" and then index from there to

the

first "<" and substring (mid) the chunk in between.



(If you might have multiple tags ahead of the target, then check if the

character following the first ">" is "<". If so, then index thereafter

for

the next ">", and so on until you find a ">" without a following "<".)



Nigel





<jcrouse1@hotmail.com>wrote in message

news:1154443004.954881.37360@i3g2000cwc.googlegroups.com...

>I am looking for a simple piece of code for stripping the XML tags from

>a string. Here is a sample string:

>

><Top>mystring</Top>

>

>I want te remove the tags and be left with this:

>

>mystring

>

>What is an easy way to do this using some sort of string manipulation?

>

>Thanks,

>John

>





-

Re:String Manipulation Question - Stripping XML Tags

A one-line version is:

myStr = Mid(myString, InStr(1, myString, ">") + 1, InStr(2, myString,

"<") - InStr(1, myString, ">") - 1)



a more simplified version is



sPos = InStr(1, myString, ">")+1

myStr = Mid(myString,sPos , InStr(2, myString, "<") -sPos )



-

Re:String Manipulation Question - Stripping XML Tags

The one-line version is far from self-documenting, and it calls Instr more

times than necessary.



I never understand why people go for one-liners. Perhaps it's a love of

acrostic puzzles?



As IBM would say KISS!



;-)





"Nicos" <nicoskk@gmail.com>wrote in message

Quote
A one-line version is:

myStr = Mid(myString, InStr(1, myString, ">") + 1, InStr(2, myString,

"<") - InStr(1, myString, ">") - 1)



a more simplified version is



sPos = InStr(1, myString, ">")+1

myStr = Mid(myString,sPos , InStr(2, myString, "<") -sPos )







-