rubylibxml-ruby

Parsing through an xml file using libxml-ruby


I'm using rails 2.3.9, ruby 1.9.3, rubygems 1.8.24 and windows 7 ultimate

I installed the libxml-ruby gem in hopes of getting the output that i want. I have an xml file that is over 150 nodes in all, and all i want is to get the value for each node.

require 'xml'
my_file = 'invoice3.xml'
parser = XML::Parser.file(my_file)
document = parser.parse
terminalId_node = document.find('//terminalId').first
terminalId = terminalId_node.content
puts terminalId

I have been able to get the value of terminalId, but I want to loop around the file to save coding time. Any help would be appreciated.

Update: Sample XML Input This is just a part of the actual xml file

<invoice>
<terminalId>68</terminalId>
<transId>8</transId>
<docDate>2012-08-06 18:55:57</docDate>
<status>P</status>
<siteId>19</siteId>
<transCode>REL</transCode>
<typeCode>POS</typeCode>
<TotalQuantity>1</TotalQuantity>
<VATRate>12</VATRate>
<amountGrossVAT>0</amountGrossVAT>
<amountGrossNonVAT>100</amountGrossNonVAT>
<amountGrossZeroRated>0</amountGrossZeroRated>
<amountGross>100</amountGross>
<amountItemDiscount>0</amountItemDiscount>
<amountOverallDiscount>0</amountOverallDiscount>
<percentOverAllDiscount>0</percentOverAllDiscount>
<OverallDiscountText></OverallDiscountText>
<amountSeniorCitizenDiscount>0</amountSeniorCitizenDiscount>
<AmountOriginalSeniorCitizenDiscount>0</AmountOriginalSeniorCitizenDiscount>
<AmountCustomerPromoDiscount>0</AmountCustomerPromoDiscount>
<amountHeaderDiscount>0</amountHeaderDiscount>
<percentHeaderDiscount>0</percentHeaderDiscount>
<invoice-details>
    <invoice-detail>    
    <amountTotalDiscount>0</amountTotalDiscount>
<percentTotalDiscount>0</percentTotalDiscount>
<amountNetVAT>0</amountNetVAT>
<amountNetNonVat>100</amountNetNonVat>
<amountNetZeroRated>0</amountNetZeroRated>
<amountNet>100</amountNet>
<AmountVatExempt>0</AmountVatExempt>
<amountDue>100</amountDue>
    <invoice-detail>
<invoice-details>
</invoice>

I want to extract ALL the data by iteration.


Solution

  • The following line retrieves all terminalId elements in your document and returns an array containing their content:

    contents = document.find('//terminalId').to_a.map(&:content)
    

    Which is equivalent to:

    contents = document.find('//terminalId').to_a.map { |e| e.content }
    

    The only difference compared to your example is that we do not use first(), which request only the first found element, but to_a() to retrieve all results.