Solved

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

Posted on 2004-10-27
251 Views
Last Modified: 2010-04-23
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 ?
0
Question by:Vartana
    25 Comments
     
    LVL 6

    Expert Comment

    by:KGreg
    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
     
    LVL 1

    Author Comment

    by:Vartana
    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
     
    LVL 1

    Author Comment

    by:Vartana
    the Response is in XML format if it helps ...
    0
     
    LVL 15

    Expert Comment

    by:Timbo87
    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
     
    LVL 1

    Author Comment

    by:Vartana
    I need it to be string parsing becuase i need to then move this to ASP thank you.
    0
     
    LVL 8

    Expert Comment

    by:toddhd
    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
     
    LVL 8

    Expert Comment

    by:toddhd
    Sorry, add this too:
    s=Replace(s,"<\","")

    and that second one should be:
    s=Replace(s,">","")
    0
     
    LVL 1

    Author Comment

    by:Vartana
    Todd i need the value between <phrase>  OK  </phrase>

    I should get the OK
    0
     
    LVL 8

    Expert Comment

    by:wguerram
    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
     
    LVL 1

    Author Comment

    by:Vartana
    how do i filter for <Phrase> Text </Phrase>
    0
     
    LVL 8

    Expert Comment

    by:toddhd
    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
     
    LVL 8

    Expert Comment

    by:wguerram
    I think i am a little confused.

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

    Text

    or

    Phrase
    0
     
    LVL 1

    Author Comment

    by:Vartana
    Text
    0
     
    LVL 8

    Expert Comment

    by:wguerram
    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
     
    LVL 1

    Author Comment

    by:Vartana
    My XML is like this

        <subresponse id="" type="">
          <transactioncodelist>
            <transactioncode id="1000">
              <phrase>OK</phrase>
              <message>Service Availability - OK</message>
            </transactioncode>
          </transactioncodelist>
          <serviceavailabilityresponse>
           
    0
     
    LVL 1

    Author Comment

    by:Vartana
    Your code is right but i dont want the value for all of them just the ones that are Phrase
    0
     
    LVL 8

    Expert Comment

    by:toddhd
    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
     
    LVL 96

    Expert Comment

    by:Bob Learned
    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
     
    LVL 8

    Accepted Solution

    by:
    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
     
    LVL 8

    Expert Comment

    by:toddhd
    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
     
    LVL 2

    Assisted Solution

    by:AmanBrar
    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
     
    LVL 18

    Expert Comment

    by:armoghan
    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
     
    LVL 18

    Expert Comment

    by:armoghan
    read the link and feedback, You will get your answer. If i have understood the question correctly :)
    0
     
    LVL 6

    Expert Comment

    by:etmendz
    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
     
    LVL 96

    Expert Comment

    by:Bob Learned
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    The Complete Ruby on Rails Developer Course

    Ruby on Rails is one of the most popular web development frameworks, and a useful tool used by both startups and more established companies to build strong graphic user interfaces, and responsive websites and apps.

    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    This video Micro Tutorial is the first in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles al…
    In this Experts Exchange video Micro Tutorial, I'm going to show how small business owners who use Google Apps can save money by setting up what is called a catch-all email address in their Gmail accounts. By using the catch-all feature, small busin…

    913 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