我有一個聽起來很基本的問題,但我還沒有找到任何解決方案。 我使用Ruby版本的XmlSimple,特別是xml_out函數。如何使用XmlSimple生成帶有屬性和文本節點的XML元素?
的問題
我無法輸出這將具有一個屬性節點和文本節點的元素。 這裏就是我想:
<lane id='1'>unchannelized</lane>
這是我目前得到:
<lane id='1'>
<content>unchannelized</content>
</lane>
我試圖用「ContentKey」 =>「內容」選項xml_out(除了「 AttrPrefix「=> true),但得到了相同的結果。我試着改變ContentKey,同樣的區別。
相關代碼
屬性&文本節點被添加到該數組:
laneConfigArr << {"@id" => laneNo, "content" => netsimLaneChannelizationCode(matchArr[matchIndex])}
正在生成的實際的哈希:
unhappyHash << {
#more stuff here,
"LaneConfig" => {"lane" => laneConfigArr},
#more stuff here
}
的xml_out通話[EDITED]:
result["NetsimLinks"] = {"NetsimLink" => unhappyHash}
doc = XmlSimple.xml_out(result, {"AttrPrefix" => true, "RootName" => "CORSIMNetwork", "ContentKey" => "content"})
環境細節
- 操作系統:Windows 7
- 紅寶石:1.9.3-P125
- XmlSimple:1.0.13
到處找,沒人似乎有這個問題。也許我錯過了一些東西,或者不能/不應該這樣做?
我非常感謝任何幫助。
你爲什麼要將輸出傳遞給REXML? – 2012-04-03 16:29:23
好處,因爲在XmlSimple引用中,它們在將REXML文檔寫入文件之前會爲其添加一個空格。我真的不在乎,所以我可以繞過它。這與問題無關。如果我不這樣做,我會得到相同的結果。 – 2012-04-03 16:35:30