Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-10-27
25
Medium Priority
?
254 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
Comment
Question by:Vartana
[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
  • 8
  • 5
  • 4
  • +6
25 Comments
 
LVL 6

Expert Comment

by:KGreg
ID: 12427281
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
ID: 12427398
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
ID: 12427410
the Response is in XML format if it helps ...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:Timbo87
ID: 12427848
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
ID: 12427981
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
ID: 12428447
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
ID: 12428459
Sorry, add this too:
s=Replace(s,"<\","")

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

Author Comment

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

I should get the OK
0
 
LVL 8

Expert Comment

by:wguerram
ID: 12428509
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
ID: 12428528
how do i filter for <Phrase> Text </Phrase>
0
 
LVL 8

Expert Comment

by:toddhd
ID: 12428535
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
ID: 12428537
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
ID: 12428543
Text
0
 
LVL 8

Expert Comment

by:wguerram
ID: 12428548
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
ID: 12428551
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
ID: 12428558
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
ID: 12428563
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
ID: 12428583
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:
wguerram earned 1000 total points
ID: 12428616
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
ID: 12428994
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
AmanBrar earned 1000 total points
ID: 12430442
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
ID: 12431685
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
ID: 12431692
read the link and feedback, You will get your answer. If i have understood the question correctly :)
0
 
LVL 6

Expert Comment

by:etmendz
ID: 12432199
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
ID: 12432449
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

604 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