1

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.

3
  • Please give us sample XML input. Also, tell us specifically what you want to extract. Commented Sep 10, 2012 at 11:43
  • I edited the question for an update, mark. Commented Sep 11, 2012 at 2:02
  • github.com/amolpujari/reading-huge-xml Commented Oct 17, 2012 at 13:55

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.