Home > Mobile >  Swift parse xml file
Swift parse xml file

Time:10-08

I am trying to parse an xml file using Swift and store it in a way that I can access data easilly later. I am specifically looking to extract the first and last name, cid nimber, and phone number. I am confused on how to do so because I have never parsed an xml file before. Any help would be appreciated!

What I've tried:

    func parseData() {
        let xmlResponseData = Bundle.main.getFileData("data.xml")
        let parser = XMLParser(data: xmlResponseData)
        parser.delegate = self
        parser.parse()
    }

XML file:

    <legislator cid="N00033987" firstlast="Doug LaMalfa" lastname="Lamalfa" party="R" office="CA01" gender="M" first_elected="2012" exit_code="0 " comments="" phone="202-225-3076" fax="530-534-7800" website="http://lamalfa.house.gov" webform="https://lamalfa.house.gov/contact/email-me" congress_office="322 Cannon House Office Building" bioguide_id="L000578" votesmart_id="29713" feccandid="H2CA02142" twitter_id="RepLaMalfa" youtube_url="https://youtube.com/RepLaMalfa" facebook_id="RepLaMalfa" birthdate="1960-07-02" />
    <legislator cid="N00033030" firstlast="Jared Huffman" lastname="Huffman" party="D" office="CA02" gender="M" first_elected="2012" exit_code="0 " comments="" phone="202-225-5161" fax="202-225-5163" website="http://huffman.house.gov" webform="https://huffman.house.gov/contact/email-me" congress_office="1406 Longworth House Office Building" bioguide_id="H001068" votesmart_id="59849" feccandid="H2CA06259" twitter_id="RepHuffman" youtube_url="https://youtube.com/rephuffman" facebook_id="RepHuffman" birthdate="1964-02-18" />
    <legislator cid="N00030856" firstlast="John Garamendi" lastname="Garamendi" party="D" office="CA03" gender="M" first_elected="2009" exit_code="0 " comments="" phone="202-225-1880" fax="202-225-5914" website="http://garamendi.house.gov" webform="https://garamendi.house.gov/contact-me/email-me" congress_office="2438 Rayburn House Office Building" bioguide_id="G000559" votesmart_id="29664" feccandid="H0CA10149" twitter_id="RepGaramendi" youtube_url="https://youtube.com/garamendiCA10" facebook_id="repgaramendi" birthdate="1945-01-24" />
    <legislator cid="N00006863" firstlast="Tom McClintock" lastname="Mcclintock" party="R" office="CA04" gender="M" first_elected="2008" exit_code="0 " comments="" phone="202-225-2511" fax="202-225-5444" website="http://mcclintock.house.gov" webform="https://mcclintockforms.house.gov/forms/contact-form.shtml" congress_office="2312 Rayburn House Office Building" bioguide_id="M001177" votesmart_id="9715" feccandid="H8CA04152" twitter_id="RepMcClintock" youtube_url="https://youtube.com/McClintockCA04" facebook_id="Congressman-Tom-McClintock-81125319109" birthdate="1956-07-10" />
    <legislator cid="N00007419" firstlast="Mike Thompson" lastname="Thompson" party="D" office="CA05" gender="M" first_elected="1998" exit_code="0 " comments="" phone="202-225-3311" fax="202-225-4335" website="http://mikethompson.house.gov" webform="https://mikethompson.house.gov/contact/email-me" congress_office="231 Cannon House Office Building" bioguide_id="T000460" votesmart_id="3564" feccandid="H8CA01109" twitter_id="RepThompson" youtube_url="https://youtube.com/CongressmanMThompson" facebook_id="RepMikeThompson" birthdate="1951-01-24" />
    <legislator cid="N00027459" firstlast="Doris Matsui" lastname="Matsui" party="D" office="CA06" gender="F" first_elected="2005" exit_code="0 " comments="" phone="202-225-7163" fax="202-225-0566" website="https://matsui.house.gov" webform="https://matsui.house.gov/email-representative-matsui" congress_office="2311 Rayburn House Office Building" bioguide_id="M001163" votesmart_id="28593" feccandid="H6CA05195" twitter_id="DorisMatsui" youtube_url="https://youtube.com/RepDorisMatsui" facebook_id="doris.matsui" birthdate="1944-09-25" />
    <legislator cid="N00030717" firstlast="Ami Bera" lastname="Bera" party="D" office="CA07" gender="M" first_elected="2012" exit_code="0 " comments="" phone="202-225-5716" fax="202-226-1298" website="http://bera.house.gov" webform="https://beraforms.house.gov/forms/writeyourrep/" congress_office="1431 Longworth House Office Building" bioguide_id="B001287" votesmart_id="120030" feccandid="H0CA03078" twitter_id="RepBera" youtube_url="https://youtube.com/repamibera" facebook_id="RepAmiBera" birthdate="1965-03-02" />
    <legislator cid="N00045377" firstlast="Jay Obernolte" lastname="Obernolte" party="R" office="CA08" gender="M" first_elected="2020" exit_code="0 " comments="" phone="202-225-5861" fax="" website="https://obernolte.house.gov/" webform="" congress_office="1029 Longworth House Office Building, Washington, DC 20515" bioguide_id="O000019" votesmart_id="" feccandid="" twitter_id="@JayObernolte" youtube_url="" facebook_id="" birthdate="08/18/70" />
    <legislator cid="N00026926" firstlast="Jerry McNerney" lastname="Mcnerny" party="D" office="CA09" gender="M" first_elected="2006" exit_code="4 " comments="Retiring at end of 117th Congress" phone="202-225-1947" fax="202-225-4060" website="http://mcnerney.house.gov" webform="https://mcnerney.house.gov/contact/email-me" congress_office="2265 Rayburn House Office Building" bioguide_id="M001166" votesmart_id="29474" feccandid="H4CA11081" twitter_id="RepMcNerney" youtube_url="https://youtube.com/RepJerryMcNerney" facebook_id="jerrymcnerney" birthdate="1951-06-18" />
    <legislator cid="N00040853" firstlast="Josh Harder" lastname="Harder" party="D" office="CA10" gender="M" first_elected="2018" exit_code="0 " comments="" phone="202-225-4540" fax="" website="https://harder.house.gov/" webform="" congress_office="131 Cannon House Office Building" bioguide_id="H001090" votesmart_id="" feccandid="" twitter_id="RepJoshHarder" youtube_url="" facebook_id="" birthdate="1986-08-01" />
    <legislator cid="N00030709" firstlast="Mark Desaulnier" lastname="Desaulnier" party="D" office="CA11" gender="M" first_elected="2014" exit_code="0 " comments="" phone="202-225-2095" fax="202-225-5609" website="https://desaulnier.house.gov" webform="" congress_office="115 Cannon House Office Building" bioguide_id="D000623" votesmart_id="" feccandid="H0CA10073" twitter_id="RepDeSaulnier" youtube_url="" facebook_id="RepMarkDeSaulnier" birthdate="1952-03-31" />
    <legislator cid="N00007360" firstlast="Nancy Pelosi" lastname="Pelosi" party="D" office="CA12" gender="F" first_elected="1987" exit_code="0 " comments="" phone="202-225-4965" fax="202-225-8259" website="http://pelosi.house.gov" webform="http://pelosi.house.gov/contact-me/email-me" congress_office="233 Cannon House Office Building" bioguide_id="P000197" votesmart_id="26732" feccandid="H8CA05035" twitter_id="NancyPelosi" youtube_url="https://youtube.com/nancypelosi" facebook_id="NancyPelosi" birthdate="1940-03-26" />
    <legislator cid="N00008046" firstlast="Barbara Lee" lastname="Lee" party="D" office="CA13" gender="F" first_elected="1998" exit_code="0 " comments="" phone="202-225-2661" fax="202-225-9817" website="https://lee.house.gov" webform="http://lee.house.gov/contact-the-office/email-me" congress_office="2267 Rayburn House Office Building" bioguide_id="L000551" votesmart_id="8315" feccandid="H8CA09060" twitter_id="RepBarbaraLee" youtube_url="https://youtube.com/RepLee" facebook_id="RepBarbaraLee" birthdate="1946-07-16" />
    <legislator cid="N00029649" firstlast="Jackie Speier" lastname="Speier" party="D" office="CA14" gender="F" first_elected="2008" exit_code="4 " comments="Retiring at end of 117th Congress" phone="202-225-3531" fax="202-347-0956" website="https://speier.house.gov" webform="https://forms.house.gov/speier/webforms/email_jackie.shtml" congress_office="2465 Rayburn House Office Building" bioguide_id="S001175" votesmart_id="8425" feccandid="H8CA12171" twitter_id="RepSpeier" youtube_url="https://youtube.com/jackiespeierca12" facebook_id="JackieSpeier" birthdate="1950-05-14" />
    <legislator cid="N00033508" firstlast="Eric Swalwell" lastname="Swalwell" party="D" office="CA15" gender="M" first_elected="2012" exit_code="0 " comments="" phone="202-225-5065" fax="" website="https://swalwell.house.gov" webform="http://swalwell.house.gov/contact-me/email-eric/" congress_office="129 Cannon House Office Building" bioguide_id="S001193" votesmart_id="129529" feccandid="H2CA15094" twitter_id="RepSwalwell" youtube_url="https://youtube.com/ericswalwell" facebook_id="CongressmanEricSwalwell" birthdate="1980-11-16" />
    <legislator cid="N00026341" firstlast="Jim Costa" lastname="Costa" party="D" office="CA16" gender="M" first_elected="2004" exit_code="0 " comments="" phone="202-225-3341" fax="202-225-9308" website="https://costa.house.gov" webform="http://www.house.gov/formcosta/issue.htm" congress_office="2081 Rayburn House Office Building" bioguide_id="C001059" votesmart_id="3577" feccandid="H4CA20082" twitter_id="RepJimCosta" youtube_url="https://youtube.com/RepJimCostaCA20" facebook_id="RepJimCosta" birthdate="1952-04-13" />
    <legislator cid="N00026427" firstlast="Ro Khanna" lastname="Khanna" party="D" office="CA17" gender="M" first_elected="2016" exit_code="0 " comments="" phone="202-225-2631" fax="" website="https://khanna.house.gov" webform="" congress_office="513 Cannon House Office Building" bioguide_id="K000389" votesmart_id="" feccandid="" twitter_id="RepRoKhanna" youtube_url="" facebook_id="RepRoKhanna" birthdate="1976-09-13" />
    <legislator cid="N00007335" firstlast="Anna Eshoo" lastname="Eshoo" party="D" office="CA18" gender="F" first_elected="1992" exit_code="0 " comments="" phone="202-225-8104" fax="202-225-8890" website="http://eshoo.house.gov" webform="https://eshoo.house.gov/email-me/" congress_office="241 Cannon House Office Building" bioguide_id="E000215" votesmart_id="26741" feccandid="H8CA12098" twitter_id="RepAnnaEshoo" youtube_url="https://youtube.com/RepAnnaEshoo" facebook_id="RepAnnaEshoo" birthdate="1942-12-13" />
    <legislator cid="N00007479" firstlast="Zoe Lofgren" lastname="Lofgren" party="D" office="CA19" gender="F" first_elected="1994" exit_code="0 " comments="" phone="202-225-3072" fax="202-225-3336" website="https://lofgren.house.gov" webform="http://lofgren.house.gov/contact/" congress_office="1401 Longworth House Office Building" bioguide_id="L000397" votesmart_id="21899" feccandid="H4CA16049" twitter_id="RepZoeLofgren" youtube_url="https://youtube.com/RepZoeLofgren" facebook_id="zoelofgren" birthdate="1947-12-21" />
    <legislator cid="N00038601" firstlast="Jimmy Panetta" lastname="Panetta" party="D" office="CA20" gender="M" first_elected="2016" exit_code="0 " comments="" phone="202-225-2861" fax="" website="https://panetta.house.gov" webform="" congress_office="228 Cannon House Office Building" bioguide_id="P000613" votesmart_id="" feccandid="" twitter_id="RepJimmyPanetta" youtube_url="" facebook_id="RepJimmyPanetta" birthdate="1969-10-01" />
    <legislator cid="N00033367" firstlast="David Valadao" lastname="Valadao" party="R" office="CA21" gender="M" first_elected="2012" exit_code="0 " comments="" phone="202-225-4695" fax="202-226-3196" website="http://valadao.house.gov" webform="http://valadao.house.gov/contact/" congress_office="1728 Longworth House Office Building" bioguide_id="V000129" votesmart_id="120200" feccandid="H2CA20094" twitter_id="RepDavidValadao" youtube_url="https://youtube.com/congressmanvaladao" facebook_id="CongressmanDavidValadao" birthdate="1977-04-14" />
    <legislator cid="N00007248" firstlast="Devin Nunes" lastname="Nunes" party="R" office="CA22" gender="M" first_elected="2002" exit_code="2 " comments="Resigned 1/3/2022" phone="202-225-2523" fax="202-225-3404" website="http://nunes.house.gov" webform="https://nunes.house.gov/contactform/default.aspx" congress_office="1013 Longworth House Office Building" bioguide_id="N000181" votesmart_id="16725" feccandid="H8CA20059" twitter_id="Rep_DevinNunes" youtube_url="https://youtube.com/RepDevinNunes" facebook_id="Congressman-Devin-Nunes-376470350795" birthdate="1973-10-01" />
    <legislator cid="N00028152" firstlast="Kevin McCarthy" lastname="Mccarthy" party="R" office="CA23" gender="M" first_elected="2006" exit_code="0 " comments="" phone="202-225-2915" fax="202-225-2908" website="http://kevinmccarthy.house.gov" webform="https://kevinmccarthy.house.gov/contact/email-me" congress_office="2421 Rayburn House Office Building" bioguide_id="M001165" votesmart_id="28918" feccandid="H6CA22125" twitter_id="GOPLeader" youtube_url="https://youtube.com/repkevinmccarthy" facebook_id="CongressmanKevinMcCarthy" birthdate="1965-01-26" />
    <legislator cid="N00037015" firstlast="Salud Carbajal" lastname="Carbajal" party="D" office="CA24" gender="M" first_elected="2016" exit_code="0 " comments="" phone="202-225-3601" fax="" website="https://carbajal.house.gov" webform="" congress_office="212 Cannon House Office Building" bioguide_id="C001112" votesmart_id="" feccandid="" twitter_id="RepCarbajal" youtube_url="" facebook_id="repsaludcarbajal" birthdate="1964-11-18" />
    <legislator cid="N00044298" firstlast="Mike Garcia" lastname="Garcia" party="R" office="CA25" gender="M" first_elected="2020" exit_code="0 " comments="" phone="" fax="" website="" webform="" congress_office="" bioguide_id="" votesmart_id="" feccandid="" twitter_id="" youtube_url="" facebook_id="" birthdate="" />
    <legislator cid="N00034254" firstlast="Julia Brownley" lastname="Brownley" party="D" office="CA26" gender="F" first_elected="2012" exit_code="0 " comments="" phone="202-225-5811" fax="202-225-1100" website="http://juliabrownley.house.gov" webform="https://juliabrownley.house.gov/contact/email-me" congress_office="1019 Longworth House Office Building" bioguide_id="B001285" votesmart_id="59904" feccandid="H2CA00120" twitter_id="JuliaBrownley26" youtube_url="https://youtube.com/RepJuliaBrownley" facebook_id="RepJuliaBrownley" birthdate="1952-08-28" />
    <legislator cid="N00030600" firstlast="Judy Chu" lastname="Chu" party="D" office="CA27" gender="F" first_elected="2009" exit_code="0 " comments="" phone="202-225-5464" fax="202-225-5467" website="https://chu.house.gov" webform="http://chu.house.gov/connect-with-me/email-judy" congress_office="2423 Rayburn House Office Building" bioguide_id="C001080" votesmart_id="16539" feccandid="H0CA32101" twitter_id="RepJudyChu" youtube_url="https://youtube.com/RepJudyChu" facebook_id="RepJudyChu" birthdate="1953-07-07" />
    <legislator cid="N00009585" firstlast="Adam Schiff" lastname="Schiff" party="D" office="CA28" gender="M" first_elected="2000" exit_code="0 " comments="" phone="202-225-4176" fax="202-225-5828" website="http://schiff.house.gov" webform="https://schiff.house.gov/email-congressman-schiff1" congress_office="2372 Rayburn House Office Building" bioguide_id="S001150" votesmart_id="9489" feccandid="H0CA27085" twitter_id="RepAdamSchiff" youtube_url="https://youtube.com/adamschiff" facebook_id="RepAdamSchiff" birthdate="1960-06-22" />
    <legislator cid="N00033373" firstlast="Tony Cardenas" lastname="Cardenas" party="D" office="CA29" gender="M" first_elected="2012" exit_code="0 " comments="" phone="202-225-6131" fax="202-225-0819" website="https://cardenas.house.gov" webform="https://cardenas.house.gov/contact/email-me" congress_office="1510 Longworth House Office Building" bioguide_id="C001097" votesmart_id="9754" feccandid="H2CA28113" twitter_id="RepCardenas" youtube_url="https://youtube.com/repcardenas" facebook_id="repcardenas" birthdate="1963-03-31" />
    <legislator cid="N00006897" firstlast="Brad Sherman" lastname="Sherman" party="D" office="CA30" gender="M" first_elected="1996" exit_code="0 " comments="" phone="202-225-5911" fax="202-225-5879" website="http://sherman.house.gov" webform="http://sherman.house.gov/contact/contact-congressman-sherman-form" congress_office="2181 Rayburn House Office Building" bioguide_id="S000344" votesmart_id="142" feccandid="H6CA24113" twitter_id="BradSherman" youtube_url="https://youtube.com/shermanca27" facebook_id="63158229861" birthdate="1954-10-24" />
    <legislator cid="N00033997" firstlast="Pete Aguilar" lastname="Aguilar" party="D" office="CA31" gender="M" first_elected="2014" exit_code="0 " comments="" phone="202-225-3201" fax="202-226-6962" website="https://aguilar.house.gov" webform="" congress_office="1223 Longworth House Office Building" bioguide_id="A000371" votesmart_id="" feccandid="H2CA31125" twitter_id="reppeteaguilar" youtube_url="" facebook_id="reppeteaguilar" birthdate="1979-06-19" />
    <legislator cid="N00006789" firstlast="Grace Napolitano" lastname="Napolitano" party="D" office="CA32" gender="F" first_elected="1998" exit_code="0 " comments="" phone="202-225-5256" fax="202-225-0027" website="http://napolitano.house.gov" webform="https://napolitano.house.gov/contact-me/email-me" congress_office="1610 Longworth House Office Building" bioguide_id="N000179" votesmart_id="8393" feccandid="H8CA34068" twitter_id="GraceNapolitano" youtube_url="https://youtube.com/RepGraceNapolitano" facebook_id="RepGraceNapolitano" birthdate="1936-12-04" />
    <legislator cid="N00035825" firstlast="Ted Lieu" lastname="Lieu" party="D" office="CA33" gender="M" first_elected="2014" exit_code="0 " comments="" phone="202-225-3976" fax="202-225-4099" website="https://lieu.house.gov" webform="" congress_office="236 Cannon House Office Building" bioguide_id="L000582" votesmart_id="" feccandid="H4CA33119" twitter_id="RepTedLieu" youtube_url="" facebook_id="RepTedLieu" birthdate="1969-03-29" />
    <legislator cid="N00040597" firstlast="Jimmy Gomez" lastname="Gomez" party="D" office="CA34" gender="M" first_elected="2017" exit_code="0 " comments="" phone="202-225-6235" fax="" website="https://gomez.house.gov" webform="" congress_office="1530 Longworth House Office Building" bioguide_id="G000585" votesmart_id="" feccandid="" twitter_id="RepJimmyGomez" youtube_url="" facebook_id="RepJimmyGomez" birthdate="1974-11-25" />
    <legislator cid="N00036107" firstlast="Norma Torres" lastname="Torres" party="D" office="CA35" gender="F" first_elected="2014" exit_code="0 " comments="" phone="202-225-6161" fax="202-225-8671" website="https://torres.house.gov" webform="" congress_office="1713 Longworth House Office Building" bioguide_id="T000474" votesmart_id="" feccandid="H4CA35031" twitter_id="NormaJTorres" youtube_url="" facebook_id="RepNormaTorres" birthdate="1965-04-04" />
    <legislator cid="N00033510" firstlast="Raul Ruiz" lastname="Ruiz" party="D" office="CA36" gender="M" first_elected="2012" exit_code="0 " comments="" phone="202-225-5330" fax="202-225-1238" website="https://ruiz.house.gov" webform="https://ruiz.house.gov/email-me" congress_office="1319 Longworth House Office Building" bioguide_id="R000599" votesmart_id="136407" feccandid="H2CA36439" twitter_id="CongressmanRuiz" youtube_url="https://youtube.com/repraulruiz" facebook_id="CongressmanRaulRuizMD" birthdate="1972-08-25" />
    <legislator cid="N00031877" firstlast="Karen Bass" lastname="Bass" party="D" office="CA37" gender="F" first_elected="2010" exit_code="3 " comments="Running for Mayor of Los Angeles" phone="202-225-7084" fax="202-225-2422" website="https://bass.house.gov" webform="https://bass.house.gov/contact-me/email-me" congress_office="2241 Rayburn House Office Building" bioguide_id="B001270" votesmart_id="28963" feccandid="H0CA33117" twitter_id="RepKarenBass" youtube_url="https://youtube.com/RepKarenBass" facebook_id="RepKarenBass" birthdate="1953-10-03" />
    <legislator cid="N00024870" firstlast="Linda Sanchez" lastname="Sanchez" party="D" office="CA38" gender="F" first_elected="2002" exit_code="0 " comments="" phone="202-225-6676" fax="202-226-1012" website="https://lindasanchez.house.gov" webform="https://forms.house.gov/lindasanchez/webforms/issue_subscribe.htm" congress_office="2329 Rayburn House Office Building" bioguide_id="S001156" votesmart_id="29674" feccandid="H2CA39078" twitter_id="RepLindaSanchez" youtube_url="https://youtube.com/LindaTSanchez" facebook_id="CongresswomanLindaSanchez" birthdate="1969-01-28" />
    <legislator cid="N00042386" firstlast="Young Kim" lastname="Kim" party="R" office="CA39" gender="F" first_elected="2020" exit_code="0 " comments="" phone="202225-4111" fax="" website="https://youngkim.house.gov/" webform="" congress_office="1306 Longworth House Office Building
Washington, DC  20515" bioguide_id="K000397" votesmart_id="151787" feccandid="" twitter_id="@RepYoungKim" youtube_url="" facebook_id="" birthdate="10/18/62" />
    <legislator cid="N00006671" firstlast="Lucille Roybal-Allard" lastname="Roybal-Allard" party="D" office="CA40" gender="F" first_elected="1992" exit_code="4 " comments="Retiring at end of 117th Congress" phone="202-225-1766" fax="202-226-0350" website="http://roybal-allard.house.gov" webform="http://roybal-allard.house.gov/contact/" congress_office="2083 Rayburn House Office Building" bioguide_id="R000486" votesmart_id="26766" feccandid="H2CA33048" twitter_id="RepRoybalAllard" youtube_url="https://youtube.com/RepRoybalAllard" facebook_id="RepRoybalAllard" birthdate="1941-06-12" />
    <legislator cid="N00006701" firstlast="Mark Takano" lastname="Takano" party="D" office="CA41" gender="M" first_elected="2012" exit_code="0 " comments="" phone="202-225-2305" fax="202-225-7018" website="https://takano.house.gov" webform="https://takano.house.gov/contact/email-me" congress_office="1507 Longworth House Office Building" bioguide_id="T000472" votesmart_id="22337" feccandid="H2CA43245" twitter_id="RepMarkTakano" youtube_url="https://youtube.com/RepMarkTakano" facebook_id="RepMarkTakano" birthdate="1960-12-10" />
    <legislator cid="N00007099" firstlast="Ken Calvert" lastname="Calvert" party="R" office="CA42" gender="M" first_elected="1992" exit_code="0 " comments="" phone="202-225-1986" fax="202-225-2004" website="http://calvert.house.gov" webform="https://calvert.house.gov/contactform/" congress_office="2205 Rayburn House Office Building" bioguide_id="C000059" votesmart_id="26777" feccandid="H2CA37023" twitter_id="KenCalvert" youtube_url="https://youtube.com/RepKenCalvert" facebook_id="RepKenCalvert" birthdate="1953-06-08" />
    <legislator cid="N00006690" firstlast="Maxine Waters" lastname="Waters" party="D" office="CA43" gender="F" first_elected="1990" exit_code="0 " comments="" phone="202-225-2201" fax="202-225-7854" website="https://waters.house.gov" webform="https://waters.house.gov/contact/contactform.htm" congress_office="2221 Rayburn House Office Building" bioguide_id="W000187" votesmart_id="26759" feccandid="H4CA23011" twitter_id="MaxineWaters" youtube_url="https://youtube.com/MaxineWaters" facebook_id="MaxineWaters" birthdate="1938-08-15" />
    <legislator cid="N00037019" firstlast="Nanette Barragan" lastname="Barragan" party="D" office="CA44" gender="F" first_elected="2016" exit_code="0 " comments="" phone="202-225-8220" fax="" website="https://barragan.house.gov" webform="" congress_office="1320 Longworth House Office Building" bioguide_id="B001300" votesmart_id="" feccandid="" twitter_id="RepBarragan" youtube_url="" facebook_id="CongresswomanBarragan" birthdate="1976-09-15" />
    <legislator cid="N00040865" firstlast="Katie Porter" lastname="Porter" party="D" office="CA45" gender="F" first_elected="2018" exit_code="0 " comments="" phone="202-225-5611" fax="" website="https://porter.house.gov/" webform="" congress_office="1117 Longworth House Office Building" bioguide_id="P000618" votesmart_id="" feccandid="" twitter_id="" youtube_url="" facebook_id="" birthdate="1974-01-03" />
    <legislator cid="N00037260" firstlast="Lou Correa" lastname="Correa" party="D" office="CA46" gender="M" first_elected="2016" exit_code="0 " comments="" phone="202-225-2965" fax="" website="https://correa.house.gov" webform="" congress_office="1039 Longworth House Office Building" bioguide_id="C001110" votesmart_id="" feccandid="" twitter_id="RepLouCorrea" youtube_url="" facebook_id="RepLouCorrea" birthdate="1958-01-24" />
    <legislator cid="N00033274" firstlast="Alan Lowenthal" lastname="Lowenthal" party="D" office="CA47" gender="M" first_elected="2012" exit_code="4 " comments="Retiring at end of 117th Congress" phone="202-225-7924" fax="202-225-7926" website="http://lowenthal.house.gov" webform="http://lowenthal.house.gov/contact/howtocontactme.htm" congress_office="125 Cannon House Office Building" bioguide_id="L000579" votesmart_id="16469" feccandid="H2CA00104" twitter_id="RepLowenthal" youtube_url="https://youtube.com/RepLowenthal" facebook_id="RepLowenthal" birthdate="1941-03-08" />
    <legislator cid="N00044501" firstlast="Michelle Steel" lastname="Steel" party="R" office="CA48" gender="F" first_elected="2020" exit_code="0 " comments="" phone="202225-2415" fax="" website="https://steel.house.gov/" webform="" congress_office="1113 Longworth House Office Building
Washington, DC  20515" bioguide_id="S001135" votesmart_id="" feccandid="" twitter_id="@RepSteel" youtube_url="" facebook_id="" birthdate="06/21/55" />
    <legislator cid="N00040667" firstlast="Mike Levin" lastname="Levin" party="D" office="CA49" gender="M" first_elected="2018" exit_code="0 " comments="" phone="202-225-3906" fax="" website="https://mikelevin.house.gov/" webform="" congress_office="1626 Longworth House Office Building" bioguide_id="L000593" votesmart_id="" feccandid="" twitter_id="RepMikeLevin" youtube_url="" facebook_id="" birthdate="1978-10-28" />
    <legislator cid="N00007017" firstlast="Darrell Issa" lastname="Issa" party="R" office="CA50" gender="M" first_elected="2000" exit_code="0 " comments="" phone="202-225-3906" fax="202-225-3303" website="https://issa.house.gov" webform="http://issa.house.gov/contact/contact-me" congress_office="2269 Rayburn House Office Building" bioguide_id="I000056" votesmart_id="16553" feccandid="H0CA48024" twitter_id="DarrellIssa" youtube_url="https://youtube.com/repdarrellissa" facebook_id="darrellissa" birthdate="1953-11-01" />
    <legislator cid="N00007021" firstlast="Juan Vargas" lastname="Vargas" party="D" office="CA51" gender="M" first_elected="2012" exit_code="0 " comments="" phone="202-225-8045" fax="202-225-9073" website="http://vargas.house.gov" webform="https://vargas.house.gov/contact" congress_office="1605 Longworth House Office Building" bioguide_id="V000130" votesmart_id="29100" feccandid="H2CA50026" twitter_id="RepJuanVargas" youtube_url="https://youtube.com/RepJuanVargas" facebook_id="RepJuanVargas" birthdate="1961-03-07" />
    <legislator cid="N00033591" firstlast="Scott Peters" lastname="Peters" party="D" office="CA52" gender="M" first_elected="2012" exit_code="0 " comments="" phone="202-225-0508" fax="202-225-2558" website="http://scottpeters.house.gov" webform="https://scottpeters.house.gov/contact/email-me" congress_office="1122 Longworth House Office Building" bioguide_id="P000608" votesmart_id="70351" feccandid="H2CA52089" twitter_id="RepScottPeters" youtube_url="" facebook_id="RepScottPeters" birthdate="1958-06-17" />
    <legislator cid="N00042081" firstlast="Sara Jacobs" lastname="Jacobs" party="D" office="CA53" gender="F" first_elected="2020" exit_code="0 " comments="" phone="202225-2040" fax="202225-2948" website="https://sarajacobs.house.gov/" webform="" congress_office="1232 Longworth HOB
Washington, DC 20515" bioguide_id="J000305" votesmart_id="" feccandid="" twitter_id="@RepSaraJacobs" youtube_url="" facebook_id="" birthdate="02/01/89" />
    <legislator cid="N00047888" firstlast="Alex Padilla" lastname="Padilla" party="D" office="CAS1" gender="M" first_elected="2021" exit_code="0 " comments="Appointed to Senate 1/20/2021" phone="202224- 3553" fax="202224-2200" website="https://www.padilla.senate.gov/" webform="" congress_office="Russell Senate Office Building
Suite B03
Washington, DC 20510" bioguide_id="P000145" votesmart_id="" feccandid="" twitter_id="@SenAlexPadilla" youtube_url="" facebook_id="" birthdate="03/22/73" />
    <legislator cid="N00036915" firstlast="Kamala Harris" lastname="Harris" party="D" office="CAS1" gender="F" first_elected="2016" exit_code="12" comments="Resigned to become Vice-President" phone="202-224-3553" fax="" website="https://www.harris.senate.gov" webform="https://www.harris.senate.gov/content/contact-senator" congress_office="112 Hart Senate Office Building" bioguide_id="H001075" votesmart_id="" feccandid="" twitter_id="SenKamalaHarris" youtube_url="" facebook_id="SenatorKamalaHarris" birthdate="1964-10-20" />
    <legislator cid="N00007364" firstlast="Dianne Feinstein" lastname="Feinstein" party="D" office="CAS2" gender="F" first_elected="1992" exit_code="0 " comments="" phone="202-224-3841" fax="202-228-3954" website="https://www.feinstein.senate.gov" webform="https://www.feinstein.senate.gov/public/index.cfm/e-mail-me" congress_office="331 Hart Senate Office Building" bioguide_id="F000062" votesmart_id="53273" feccandid="S0CA00199" twitter_id="SenFeinstein" youtube_url="https://youtube.com/SenatorFeinstein" facebook_id="senatorfeinstein" birthdate="1933-06-22" />
</response>```

CodePudding user response:

Try this example code (recycled from my comment first link), works for me:

import Foundation
import SwiftUI

struct Legislator: Identifiable {
    let id = UUID()
    var cid: String
    var firstlast: String
    var lastname: String
    var party: String
    var office: String
    var gender: String
    var first_elected: String
    var exit_code: String
    var comments: String
    var phone: String
    var fax: String
    var website: String
    var webform: String
    var congress_office: String
    var bioguide_id: String
    var votesmart_id: String
    var feccandid: String
    var twitter_id: String
    var youtube_url: String
    var facebook_id: String
    var birthdate: String
}

struct ContentView: View {
    @State var legislators: [Legislator] = []

    var body: some View {
        VStack {
            List(legislators) { legist in
                HStack {
                    Text(legist.firstlast)
                    Text("phone: "   legist.phone)
                    Text(legist.cid).foregroundColor(.blue)
                }
            }
        }
        .onAppear {
            if let url = Bundle.main.url(forResource: "data", withExtension: "xml") {
                do {
                    let data = try Data(contentsOf: url)
                    let parser = LegislatorParser(data: data)
                    if parser.parse() {
                        legislators = parser.legislators
                    } else {
                        print("---> parser error: \(parser.parserError as Optional)")
                    }
                } catch {
                    print("---> data error: \(error)")
                }
            }
        }
    }
    
}

class LegislatorParser: XMLParser {
    var legislators: [Legislator] = []

    override init(data: Data) {
        super.init(data: data)
        self.delegate = self
    }
}

extension LegislatorParser: XMLParserDelegate {
    
    // Called when opening tag (`<elementName>`) is found
    func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
        
        switch elementName {
        case "legislator":
            let legist = Legislator(
                cid: attributeDict["cid"] ?? "",
                firstlast: attributeDict["firstlast"] ?? "",
                lastname: attributeDict["lastname"] ?? "",
                party: attributeDict["party"] ?? "",
                office: attributeDict["office"] ?? "",
                gender: attributeDict["gender"] ?? "",
                first_elected: attributeDict["first_elected"] ?? "",
                exit_code: attributeDict["exit_code"] ?? "",
                comments: attributeDict["comments"] ?? "",
                phone: attributeDict["phone"] ?? "",
                fax: attributeDict["fax"] ?? "",
                website: attributeDict["website"] ?? "",
                webform: attributeDict["webform"] ?? "",
                congress_office: attributeDict["congress_office"] ?? "",
                bioguide_id: attributeDict["bioguide_id"] ?? "",
                votesmart_id: attributeDict["votesmart_id"] ?? "",
                feccandid: attributeDict["feccandid"] ?? "",
                twitter_id: attributeDict["twitter_id"] ?? "",
                youtube_url: attributeDict["youtube_url"] ?? "",
                facebook_id: attributeDict["facebook_id"] ?? "",
                birthdate: attributeDict["birthdate"] ?? ""
            )
            legislators.append(legist)
            
        default: break
        }
    }
    
    // Called when closing tag (`</elementName>`) is found
    func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
        //...
    }
    
    // Called when a character sequence is found
    func parser(_ parser: XMLParser, foundCharacters string: String) {
        //...
    }
    
    // Called when a CDATA block is found
    func parser(_ parser: XMLParser, foundCDATA CDATABlock: Data) {
        guard String(data: CDATABlock, encoding: .utf8) != nil else {
            print("CDATA contains non-textual data, ignored")
            return
        }
    }
}

The data.xml file

<?xml version="1.0" encoding="utf-8"?>
<response>
<legislator cid="N00033987" firstlast="Doug LaMalfa" lastname="Lamalfa" party="R" office="CA01" gender="M" first_elected="2012" exit_code="0 " comments="" phone="202-225-3076" fax="530-534-7800" website="http://lamalfa.house.gov" webform="https://lamalfa.house.gov/contact/email-me" congress_office="322 Cannon House Office Building" bioguide_id="L000578" votesmart_id="29713" feccandid="H2CA02142" twitter_id="RepLaMalfa" youtube_url="https://youtube.com/RepLaMalfa" facebook_id="RepLaMalfa" birthdate="1960-07-02" />
 ....
</response>
  • Related