问题
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
}
I was able to access elements with results:
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
How can I change the code, so that I would have results like? :
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
In other words I want to access elements in the same sequence they appear in xml file.
回答1:
You need to iterate over Header
and Line
elements
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}" }
}
Output:
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
来源:https://stackoverflow.com/questions/16810539/accessing-xml-file-with-rexml