walterf25 Expert Licensed User Longtime User Feb 1, 2012 #1 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, Code: <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 Code: 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:
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, Code: <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 Code: 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:
W wl Well-Known Member Licensed User Longtime User Feb 2, 2012 #2 I would use an existing SAX or DOM library to do the parsing. Although I have not used it myself on B4A, I'd look at: http://www.b4x.com/forum/basic4andr...866-xml-parsing-xmlsax-library.html#post39737 Wim Upvote 0
I would use an existing SAX or DOM library to do the parsing. Although I have not used it myself on B4A, I'd look at: http://www.b4x.com/forum/basic4andr...866-xml-parsing-xmlsax-library.html#post39737 Wim
J jfranz Member Licensed User Longtime User Feb 2, 2012 #3 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
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.
walterf25 Expert Licensed User Longtime User Feb 2, 2012 #4 Thanks a lot guys, i will give both of your suggestions a try and will post back with feedback. Thanks, Upvote 0
Thanks a lot guys, i will give both of your suggestions a try and will post back with feedback. Thanks,
walterf25 Expert Licensed User Longtime User Feb 2, 2012 #5 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
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,
Erel B4X founder Staff member Licensed User Longtime User Feb 3, 2012 #6 You are posting the same question in multiple threads. I've already answered to your question in the "tutorial" thread. You should avoid doing this. You should use StartElement to read the attributes. Upvote 0
You are posting the same question in multiple threads. I've already answered to your question in the "tutorial" thread. You should avoid doing this. You should use StartElement to read the attributes.
walterf25 Expert Licensed User Longtime User Feb 3, 2012 #7 Sorry about that Erel, and thanks for your response, can you give me an example as to how to get the attributes please? thanks,:sign0098: Upvote 0
Sorry about that Erel, and thanks for your response, can you give me an example as to how to get the attributes please? thanks,:sign0098:
Erel B4X founder Staff member Licensed User Longtime User Feb 5, 2012 #8 Something like: Code: Sub Parser_StartElement (Uri As String, Name As String, Attributes As Attributes) If Name = "rep" Then GlobalName = Attributes.GetValue2("", "name") GlobalParty = Attributes.GetValue2("", "party") End If End Sub Upvote 0
Something like: Code: Sub Parser_StartElement (Uri As String, Name As String, Attributes As Attributes) If Name = "rep" Then GlobalName = Attributes.GetValue2("", "name") GlobalParty = Attributes.GetValue2("", "party") End If End Sub