Get the text between <>MyTExt</> text string

I have a string that has  a formate like so

        <street1>88989 dddd st</street1>
        <street2>Apt 101</street2>
        <city>Glendale</city>
        <state>CA</state>
        <zip>91606</zip>

How can i grab the text between <> </> that i want ?
LVL 1
VartanaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KGregCommented:
You need to use regular expressions:

Make sure you add this:

Imports System.Text.RegularExpressions

Then use this code to parse the text:

        Dim strPattern As String = "<[^>]*?>(?<mytext>[^<]*)<\/<[^>]*?>>"
        Dim objReg As Regex = New Regex(strPattern, RegexOptions.IgnoreCase)
        Dim m As Match = objReg.Match(strTextToParse)

        While m.Success
            MsgBox(m.Groups("mytext"))
            m = m.NextMatch
        End While


Hope this helps


KGREG
0
VartanaAuthor Commented:
It did not work

    <transactioncodelist>
      <transactioncode id="100">
        <phrase>OK</phrase>
        <message>Request was a complete success.</message>
      </transactioncode>
    </transactioncodelist>

Code:
        Dim strPattern As String = "<[^>]*?>(?<phrase>[^<]*)<\/<[^>]*?>>"
        Dim objReg As Regex = New Regex(strPattern, RegexOptions.IgnoreCase)
        Dim m As Match = objReg.Match(Response)

        While m.Success
            MsgBox(m.Groups("phrase"))
            m = m.NextMatch
        End While
0
VartanaAuthor Commented:
the Response is in XML format if it helps ...
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Timbo87Commented:
You can use the XmlDocument class with XPath. This example gets the first message element.

Imports System.Xml

Dim doc As New XmlDocument()
doc.LoadXml(xmlString)
Dim messages As XmlNodeList = doc.SelectNodes("/transactioncodelist/transactioncode/message")

To get the first node:
Dim content As String = messages(0).InnerText
Console.WriteLine("The contents of the first message element is " & content)

Output:
The contents of the first message is Request was a complete success.

You can learn more about XPath here:
http://www.w3schools.com/xpath/default.asp
0
VartanaAuthor Commented:
I need it to be string parsing becuase i need to then move this to ASP thank you.
0
toddhdCommented:
You could always just do this:

Dim s as string = "<whatever>" ' Assign your XML value here
s = Replace(s,"<","")
s=Replace(s,">")

That would remove the brackets so that all you are left with is the text. It might make some grimace, but it would work.
0
toddhdCommented:
Sorry, add this too:
s=Replace(s,"<\","")

and that second one should be:
s=Replace(s,">","")
0
VartanaAuthor Commented:
Todd i need the value between <phrase>  OK  </phrase>

I should get the OK
0
wguerramCommented:
Assuming you are reading line by line from the file:

       Dim s As String
       Dim intStart, intEnd As Integer

        s = "<street1>88989 dddd st</street1>"
        intStart = s.IndexOf(">") + 1
        intEnd = s.IndexOf("<", intStart)

        MsgBox(s.Substring(intStart, intEnd - intStart))
0
VartanaAuthor Commented:
how do i filter for <Phrase> Text </Phrase>
0
toddhdCommented:
Sorry about that.

To be honest, Regular expression are the right way to go, and there is no reason you can't use them just because you are using ASP. ASP supports regular expressions too. Here is an example page:

http://www.4guysfromrolla.com/demos/phoneFormat.asp

0
wguerramCommented:
I think i am a little confused.

What do you need from <Phrase> Text </Phrase>?

Text

or

Phrase
0
VartanaAuthor Commented:
Text
0
wguerramCommented:
Then try my code

Assuming you are reading line by line from the file:

       Dim s As String
       Dim intStart, intEnd As Integer

        s = "<street1>88989 dddd st</street1>"
        intStart = s.IndexOf(">") + 1
        intEnd = s.IndexOf("<", intStart)

        MsgBox(s.Substring(intStart, intEnd - intStart))

I test it and work fines.
0
VartanaAuthor Commented:
My XML is like this

    <subresponse id="" type="">
      <transactioncodelist>
        <transactioncode id="1000">
          <phrase>OK</phrase>
          <message>Service Availability - OK</message>
        </transactioncode>
      </transactioncodelist>
      <serviceavailabilityresponse>
       
0
VartanaAuthor Commented:
Your code is right but i dont want the value for all of them just the ones that are Phrase
0
toddhdCommented:
Private Function GetValue(Byval s as string) as String
       Dim intStart, intEnd As Integer

        intStart = s.IndexOf(">") + 1
        intEnd = s.IndexOf("<", intStart)

        Return s.Substring(intStart, intEnd - intStart)
End Function

        s = "<Phrase> Text </Phrase>"
Myvalue=GetValue(s)

0
Bob LearnedCommented:
Timbo87's suggestion is a much better approach with XML parsing because you can target nodes very easily and get the values from the <Phrase> nodes.

Bob
0
wguerramCommented:
Bob is right. In case you decide to use my code:

you can check if the "Phrase" exist in order to Call the code i suggested:

s = "<Phrase> Text </Phrase>"
     If s.ToLower.IndexOf("phrase") > 0 Then
           Myvalue=GetValue(s)
     end if

Private Function GetValue(Byval s as string) as String
       Dim intStart, intEnd As Integer

        intStart = s.IndexOf(">") + 1
        intEnd = s.IndexOf("<", intStart)

        Return s.Substring(intStart, intEnd - intStart)
End Function

     
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
toddhdCommented:
You have 3 good options now - XML Parsing and Regular expressions are the best way to go, but may be overkill depending on your application and expereince. wguerram's code is a simple and viable option that should port to ASP easily (you'll have to change stuff like s.IndexOf() to Instr(), but you get the idea). Pick one and run with it, because it won't get any simpler that those.
0
AmanBrarCommented:
Hi,

If you want to parse an XML, you may use XMLDocument:

    Dim m_xmld As XmlDocument
    Dim m_nodelist As XmlNodeList
    Dim m_node As XmlNode
   
    'Create the XML Document
    m_xmld = New XmlDocument()
   
    'Load the Xml file
    m_xmld.Load("PUT FILEPATH HERE")

    'Get the list of nodes
    m_nodelist = m_xmld.SelectNodes("/subresponse/transactioncodelist/transactioncode")

    'Loop through the nodes
    For Each m_node In m_nodelist
      'Get the phrase element Value
      Dim phraseValue = m_node.ChildNodes.Item(0).InnerText

      'Get the message element Value
      Dim messageValue = m_node.ChildNodes.Item(1).InnerText

      'Do something with the data
      Console.Write("Phrase: " & phraseValue)
      Console.Write(vbCrLf)
      Console.Write("Message: " & messageValue)
      Console.Write(vbCrLf)
    Next

hope this helps.
0
armoghanCommented:
I think what you want is to remove all the <TAGS> </TAGS>
i.e. what ever is like <xxxxxxxxxx> or </xxxxxxxxxxx>

for that see this article

http://weblogs.asp.net/rosherove/archive/2003/05/13/6963.aspx
0
armoghanCommented:
read the link and feedback, You will get your answer. If i have understood the question correctly :)
0
etmendzCommented:
There should be no problem using the XML parsing techniques described here even in ASP. MSXML can be accessed in ASP. Get the solution here:

http://www.15seconds.com/issue/990527.htm

It shows a code where it gets ALL instances of an element in the document and extracts the value of that element.

Have fun.
0
Bob LearnedCommented:
I agree that wguerram's sample is simple, but I believe for your application that it is too simple, and doesn't provide for getting node information, especially if you have to search for multiple nodes.

IMHO XPath expressions are both simple and powerful, and fit the task of getting information from XML files, versus using Regular Expressions.  So, take the time to learn something new, and it will pay great dividends in the future.

Bob
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.