如何使用ruby循环xml节点

时间:2019-01-09 07:32:16

标签: ruby xml csv xml-parsing nokogiri

我有一个50MB的XML文件。我需要解析它并转换为CSV。 以下是XML File

<?xml version="1.0" encoding="ISO-8859-1"?>
<IAPDFirmStateReport GenOn="2019-01-02">
<Firms>
<Firm>
<Info FirmCrdNb="146099" BusNm="PRINCIPA FINANCIAL ADVISORS" LegalNm="CHUNG, BUCK CHWEE"/>
<MainAddr Strt1="15111 WHITTIER BLVD" Strt2="STE 420" City="WHITTIER" State="CA" Cntry="United States" PostlCd="90603" PhNb="562-945-7888" FaxNb="562-968-1885"/>
<MailingAddr/>
<StateRgstn>
<Rgltrs>
<Rgltr Cd="CA" St="APPROVED" Dt="2008-03-13"/>
</Rgltrs>
</StateRgstn>
<ERA>
<Rgltrs/>
</ERA>
</Firm>
<Firm>
<Info FirmCrdNb="170562" SECNb="802-112318" BusNm="ALUMNI VENTURES GROUP" LegalNm="LAUNCH ANGELS MANAGEMENT COMPANY, LLC"/>
<MainAddr Strt1="788 ELM ST" City="MANCHESTER" State="NH" Cntry="United States" PostlCd="03101" PhNb="603-518-8112"/>
<MailingAddr Strt1="889 ELM ST" Strt2="3RD FLOOR" City="MANCHESTER" State="NH" Cntry="United States" PostlCd="03101"/>
<StateRgstn>
<Rgltrs/>
</StateRgstn>
<ERA>
<Rgltrs>
<Rgltr Cd="MA" St="ACTIVE" Dt="2014-02-24"/>
<Rgltr Cd="NH" St="ACTIVE" Dt="2018-07-23"/>
</Rgltrs>
</ERA>
</Firm>
<`/Frims>`

------几乎有90个牢固的标签

因此,我需要使用ruby动态解析它并将其转换为CSV。我该如何解决?

1 个答案:

答案 0 :(得分:1)

看看这个问题-Parsing XML with Ruby

您也可以使用上述问题中未提及的Ox宝石-在此处查看: https://github.com/ohler55/ox#parsing-xml-into-a-hash-fast

一旦将XML转换为Hash,就应该轻松将其转换为CSV