使用REXML访问xml文件

时间:2013-05-29 09:43:00

标签: ruby rexml

sample.xml中

<?xml version="1.0" encoding="utf-8"?>
<ShipmentRequest>
   <Message>
      <Header>
      <MemberId>MID-0000001</MemberId>    
      <MemberName>Bruce</MemberName>
      <DeliveryId>0000001</DeliveryId>
      <OrderNumber>ON-000000001</OrderNumber>
      <ShipToName>Alan</ShipToName>
      <ShipToZip>123-4567</ShipToZip>
      <ShipToStreet>West</ShipToStreet>
      <ShipToCity>Seatle</ShipToCity>
       <Payments>
        <PayType>Credit Card</PayType>
        <Amount>20</Amount>
      </Payments>
      <Payments>
        <PayType>Points</PayType>
        <Amount>22</Amount>
      </Payments>
      <PayType />
      </Header>
    <Line>
      <LineNumber>3.1</LineNumber>
      <ItemId>A-0000001</ItemId>
      <Description>Apple</Description>
      <Quantity>2</Quantity>
      <UnitCost>5</UnitCost>
    </Line>
    <Line>
      <LineNumber>4.1</LineNumber>
      <ItemId>P-0000001</ItemId>
      <Description>Peach</Description>
      <Quantity>4</Quantity>
      <UnitCost>6</UnitCost>
    </Line>
    <Line>
      <LineNumber>5.1</LineNumber>
      <ItemId>O-0000001</ItemId>
      <Description>Orange</Description>
      <Quantity>2</Quantity>
      <UnitCost>4</UnitCost>
    </Line>
  </Message>
</ShipmentRequest>

xml.rb

#!/usr/bin/ruby -w

require 'rexml/document'
include REXML

xmlfile = File.new("sample.xml")
xmldoc = Document.new(xmlfile)

puts "MemberId:" + xmldoc.elements().to_a("ShipmentRequest/Message/Header/MemberId").first.text

puts "MemberName:" + xmldoc.elements().to_a("ShipmentRequest/Message/Header/MemberName").first.text

puts "DeliveryId:" + xmldoc.elements().to_a("ShipmentRequest/Message/Header/DeliveryId").first.text

puts "ShipToName: " + xmldoc.elements().to_a("ShipmentRequest/Message/Header/ShipToName").first.text
puts "ShipToZip: " + xmldoc.elements().to_a("ShipmentRequest/Message/Header/ShipToZip").first.text
puts "ShipToStreet: " + xmldoc.elements().to_a("ShipmentRequest/Message/Header/ShipToStreet").first.text
puts "ShipToCity: " + xmldoc.elements().to_a("ShipmentRequest/Message/Header/ShipToCity").first.text

xmldoc.elements.each("ShipmentRequest/Message/Line/LineNumber") {
    |e| puts "LineNumber: " + e.text
}

xmldoc.elements.each("ShipmentRequest/Message/Line/ItemId") {
    |e| puts "ItemId: " + e.text
}

xmldoc.elements.each("ShipmentRequest/Message/Line/Description") {
    |e| puts "Description: " + e.text
}

xmldoc.elements.each("ShipmentRequest/Message/Line/Quantity") {
    |e| puts "Quantity: " + e.text
}

xmldoc.elements.each("ShipmentRequest/Message/Line/UnitCost") {
    |e| puts "UnitCost: " + e.text
}

我能够访问带有结果的元素:

MemberId:MID-0000001
MemberName:Bruce
DeliveryId:0000001
ShipToName: Alan
ShipToZip: 123-4567
ShipToStreet: West
ShipToCity: Seatle
LineNumber: 3.1
LineNumber: 4.1
LineNumber: 5.1
ItemId: A-0000001
ItemId: P-0000001
ItemId: O-0000001
Description: Apple
Description: Peach
Description: Orange
Quantity: 2
Quantity: 4
Quantity: 2
UnitCost: 5
UnitCost: 6
UnitCost: 4

如何更改代码,以便获得结果? :

MemberId:MID-0000001
MemberName:Bruce
DeliveryId:0000001
ShipToName: Alan
ShipToZip: 123-4567
ShipToStreet: West
ShipToCity: Seatle
LineNumber: 3.1
ItemId: A-0000001
Description: Apple
Quantity: 2
UnitCost: 5
LineNumber: 4.1
ItemId: P-0000001
Description: Peach
Quantity: 4
UnitCost: 6
LineNumber: 5.1
ItemId: O-0000001
Description: Orange
Quantity: 2
UnitCost: 4

换句话说,我想按照它们在xml文件中出现的相同顺序访问元素。

1 个答案:

答案 0 :(得分:1)

您需要迭代HeaderLine元素

xmldoc.elements.each("ShipmentRequest/Message/Header")  {
 |e| e.elements.each {|i| next if i.name.to_s.match(/Payments|PayType/); puts "#{i.name} : #{i.text}" }
}

xmldoc.elements.each("ShipmentRequest/Message/Line")  {
 |e| e.elements.each {|i| puts "#{i.name} : #{i.text}" }
}

输出:

MemberId : MID-0000001
MemberName : Bruce
DeliveryId : 0000001
OrderNumber : ON-000000001
ShipToName : Alan
ShipToZip : 123-4567
ShipToStreet : West
ShipToCity : Seatle
LineNumber : 3.1
ItemId : A-0000001
Description : Apple
Quantity : 2
UnitCost : 5
LineNumber : 4.1
ItemId : P-0000001
Description : Peach
Quantity : 4
UnitCost : 6
LineNumber : 5.1
ItemId : O-0000001
Description : Orange
Quantity : 2
UnitCost : 4