我也有類似的爲*的.sln格式的配置格式,所以採取以下爲例:如何解析文本中的多級「節點」?
DCOM Productions Configuration File, Format Version 1.0
BeginSection:Global
GlobalKeyA = AnswerOne
.: Stores the global configuration key
:: for the application. This key is used
:: to save the current state of the app.
:: as well as prevent lockups
GlobalKey3 = AnswerTwo
.: Secondary Key. See above setting
GlobalKeyC = AnswerThree
BeginSection: UpdateSystem
NestedKeyA = One
NestedKeyB = Two
NestedKeyC = { A set of multiline data
where we will show how
to write a multiline
paragraph }
NestedKeyD = System.Int32, 100
EndSection
EndSection
BeginSection:Application
InstallPath = C:\Program Files\DCOM Productions\BitFlex
EndSection
我知道我需要一個遞歸函數可能是採用文字的段作爲參數的話,例如,將整個部分傳遞給它,並遞歸地解析它。
我似乎無法讓我的腦袋圍繞如何做到這一點。每個部分可能會有更多的子部分。這就像一個Xml文檔..我沒有真正要求代碼,只是一個關於如何解析文檔的方法。
我正在考慮使用選項卡(指定索引)來確定我正在使用哪一節,但如果文檔沒有正確選項卡(格式化),則會失敗。有更好的想法嗎?
呀,開始和EndSection的基本上都是年底開始停止節點,但我會如何區分EndSection之間屬於哪個BeginSection?我不能只抓住第一個,因爲它可能是嵌套節點的EndSection,而不是第一個被解析的節點。 – 2009-07-25 01:15:33