ここでは、Ruby 用の mongodb ドライバーを使用しています。しかし、これが完全に機能するようになったら、Mongoid ODB を使用して Ruby on Rails 3 でスケジュールされたタスクとして実行したいと考えています。
今のところ、私はルビーで実験しています。
XML ファイルを mongodb に挿入できる形式に変換するには、crack gem が非常に便利であることに気付きました。ruby 用の mongodb ドライバーを使用すると、クラックは JSON に近い形式に変換します (":" 列の代わりに "=>" を使用しています)。
私がクラックを使用している方法の問題は、XMLファイルにあるすべてのものをインポートします。下記を参照してください。
サンプル.xml
<?xml version="1.0" encoding="utf-8"?>
<ShipmentRequest>
<Envelope>
<TransmissionDateTime>05/08/2013 23:06:02</TransmissionDateTime>
</Envelope>
<Message>
<Comment />
<Header>
<MemberId>A00000001</MemberId>
<MemberName>Bruce</MemberName>
<DeliveryId>6377935</DeliveryId>
<ShipToAddress1>123-4567</ShipToAddress1>
<OrderDate>05/08/13</OrderDate>
<Payments>
<PayType>Credit Card</PayType>
<Amount>1000</Amount>
</Payments>
<Payments>
<PayType>Points</PayType>
<Amount>5390</Amount>
</Payments>
</Header>
<Line>
<LineNumber>3.1</LineNumber>
<Item>fruit-004</Item>
<Description>Peach</Description>
<Quantity>1</Quantity>
<UnitCost>1610</UnitCost>
<DeclaredValue>0</DeclaredValue>
<PointValue>13</PointValue>
</Line>
<Line>
<LineNumber>8.1</LineNumber>
<Item>fruit-001</Item>
<Description>Fruit Set</Description>
<Quantity>1</Quantity>
<UnitCost>23550</UnitCost>
<PointValue>105</PointValue>
<PickLine>
<PickLineNumber>8.1..1</PickLineNumber>
<PickItem>fruit-002</PickItem>
<PickDescription>Apple</PickDescription>
<PickQuantity>1</PickQuantity>
</PickLine>
<PickLine>
<PickLineNumber>8.1..2</PickLineNumber>
<PickItem>fruit-003</PickItem>
<PickDescription>Orange</PickDescription>
<PickQuantity>2</PickQuantity>
</PickLine>
</Line>
</Message>
</ShipmentRequest>
sample_crack.rb
#!/usr/bin/ruby
require "crack"
require 'mongo'
include Mongo
mongo_client = MongoClient.new("localhost", 27017)
db = mongo_client.db("somedb")
coll = db.collection("somecoll")
myXML = Crack::XML.parse(File.read("sample.xml"))
coll.insert(myXML)
puts myXML
コンソールに出力されます:
{"ShipmentRequest"=>{"Envelope"=>{"TransmissionDateTime"=>"05/08/2013 23:06:02"}, "Message"=>{"Comment"=>nil, "Header"=>{"MemberId"=>"A00000001", "MemberName"=>"Bruce", "DeliveryId"=>"6377935", "ShipToAddress1"=>"123-4567", "OrderDate"=>"05/08/13", "Payments"=>[{"PayType"=>"Credit Card", "Amount"=>"1000"}, {"PayType"=>"Points", "Amount"=>"5390"}]}, "Line"=>[{"LineNumber"=>"3.1", "Item"=>"fruit-004", "Description"=>"Peach", "Quantity"=>"1", "UnitCost"=>"1610", "DeclaredValue"=>"0", "PointValue"=>"13"}, {"LineNumber"=>"8.1", "Item"=>"fruit-001", "Description"=>"Fruit Set", "Quantity"=>"1", "UnitCost"=>"23550", "PointValue"=>"105", "PickLine"=>[{"PickLineNumber"=>"8.1..1", "PickItem"=>"fruit-002", "PickDescription"=>"Apple", "PickQuantity"=>"1"}, {"PickLineNumber"=>"8.1..2", "PickItem"=>"fruit-003", "PickDescription"=>"Orange", "PickQuantity"=>"2"}]}]}}, :_id=>BSON::ObjectId('51ad8d83a3d24b3b9f000001')}
mongodb では、変換された XML ファイルは次のようになります。
{
"_id" : ObjectId("51ad8d83a3d24b3b9f000001"),
"ShipmentRequest" : {
"Envelope" : {
"TransmissionDateTime" : "05/08/2013 23:06:02"
},
"Message" : {
"Comment" : null,
"Header" : {
"MemberId" : "A00000001",
"MemberName" : "Bruce",
"DeliveryId" : "6377935",
"ShipToAddress1" : "123-4567",
"OrderDate" : "05/08/13",
"Payments" : [
{
"PayType" : "Credit Card",
"Amount" : "1000"
},
{
"PayType" : "Points",
"Amount" : "5390"
}
]
},
"Line" : [
{
"LineNumber" : "3.1",
"Item" : "fruit-004",
"Description" : "Peach",
"Quantity" : "1",
"UnitCost" : "1610",
"DeclaredValue" : "0",
"PointValue" : "13"
},
{
"LineNumber" : "8.1",
"Item" : "fruit-001",
"Description" : "Fruit Set",
"Quantity" : "1",
"UnitCost" : "23550",
"PointValue" : "105",
"PickLine" : [
{
"PickLineNumber" : "8.1..1",
"PickItem" : "fruit-002",
"PickDescription" : "Apple",
"PickQuantity" : "1"
},
{
"PickLineNumber" : "8.1..2",
"PickItem" : "fruit-003",
"PickDescription" : "Orange",
"PickQuantity" : "2"
}
]
}
]
}
}
}
しかし、不要なノードを削除して空のノードを無視するようにインポートしたいと思います。
{
"_id" : ObjectId("51ad8d83a3d24b3b9f000001"),
"MemberId" : "A00000001",
"MemberName" : "Bruce",
"DeliveryId" : "6377935",
"ShipToAddress1" : "123-4567",
"OrderDate" : "05/08/13",
"Payments" : [
{
"PayType" : "Credit Card",
"Amount" : "1000"
},
{
"PayType" : "Points",
"Amount" : "5390"
}
],
"Line" : [
{
"LineNumber" : "3.1",
"Item" : "fruit-004",
"Description" : "Peach",
"Quantity" : "1",
"UnitCost" : "1610",
"DeclaredValue" : "0",
"PointValue" : "13"
},
{
"LineNumber" : "8.1",
"Item" : "fruit-001",
"Description" : "Fruit Set",
"Quantity" : "1",
"UnitCost" : "23550",
"PointValue" : "105",
"PickLine" : [
{
"PickLineNumber" : "8.1..1",
"PickItem" : "fruit-002",
"PickDescription" : "Apple",
"PickQuantity" : "1"
},
{
"PickLineNumber" : "8.1..2",
"PickItem" : "fruit-003",
"PickDescription" : "Orange",
"PickQuantity" : "2"
}
]
}
]
}
これはクラックでできますか?それともノコギリの方がいいですか?
アップデート
@Alex Peachey に感謝します。更新されたコードをここに置きます。
sample_crack/rb (更新):
#!/usr/bin/ruby
require "crack"
require 'mongo'
include Mongo
mongo_client = MongoClient.new("localhost", 27017)
db = mongo_client.db("somedb")
coll = db.collection("somecoll")
myXML = Crack::XML.parse(File.read("sample.xml"))
myXML.merge!(myXML.delete("ShipmentRequest")) # not needed hash
myXML.merge!(myXML.delete("Message")) # not needed hash
myXML.merge!(myXML.delete("Header")) # not needed hash
myXML.delete("Envelope") # not needed hash
# planning to put here a code to remove hashes with empty values
coll.insert(myXML)
puts myXML