XML Parsing

walterf25

Expert
Licensed User
Longtime User
Hello guys, i was wondering if anyone here can help me out figure out how to parse an xml portion below is the exact portion i'm working with, my code is working i get no error but i get nothing,

B4X:
<result>
<rep name="Brad Sherman" party="D" state="CA" district="27" phone="202-225-5911" office="2242 Rayburn House Office Building" link="http://bradsherman.house.gov/"/>
<rep name="Howard Berman" party="D" state="CA" district="28" phone="202-225-4695" office="2221 Rayburn House Office Building" link="http://www.house.gov/berman/"/>
<rep name="Barbara Boxer" party="D" state="CA" district="Junior Seat" phone="202-224-3553" office="112 Hart Senate Office Building" link="http://boxer.senate.gov"/>
<rep name="Dianne Feinstein" party="D" state="CA" district="Senior Seat" phone="202-224-3841" office="331 Hart Senate Office Building" link="http://feinstein.senate.gov"/>
</result>
here's my portion of my code that takes care of the parsing
can anyone look at it and maybe give me some pointers, i know i must be missing something, any and all help will be greatly appreciated

B4X:
Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
Dim replistview1 As List
replistview1.Initialize
    If parser.Parents.IndexOf("result") > -1 Then
        If Name = "rep" Then
            repname = Text.ToString
        End If
    End If
    If Name = "result" Then
    ToastMessageShow(repname, True)
    End If


End Sub

i just need to retrieve the representative names and maybe the district, can anyone please help me figure this out?

thanks,
:sign0163:
 

jfranz

Member
Licensed User
Longtime User
Simple Regex pattern would work also if just a name

If you don't like XML parsing than you could do it using this regex pattern: <rep\sname=\x22(.*?)\x22

Note: the \x22 is used instead of " because of the string that needs to get supplied in basic.

Dim match As Matcher
Dim str As String
match = Regex.Matcher("<rep\sname=\x22(.*?)\x22", xmlstr)
Do While match.Find
str = match.Group(1)
If str <> "" Then
' Do something with the name here
End If
Loop

I don't know what is easier but sometimes XML gets a little tough when multiple nested nodes.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Ok jfrankz, i tried your suggestion but i get an error saying that no match found yet, i copied the code exactly as you typed it in your post, can you help me figure this out?

Thanks,
 
Upvote 0
Top