我可以从我们专有的CNC编程软件导出对象的序列化文本表示,并需要解析它以在我的Rails应用程序中导入对象。
示例序列化输出:
Header {
code "Centric 20170117 16gaHRS"
label "Centric 20170117 16gaHRS"
lccShortname "Centric 20170117 16gaHRS"
jobgroup "20170117 - Pike Sign"
waste 97.5516173272
unit INCH
Material {
code "HRS"
label "HRS"
labelDIN "HRS"
density 0.283647787542
thickness 0.125
}
}
Rawmaterials {
Rawmaterial {
id 52312
format 120 48.25
stock +999
used +1
}
}
Parts {
Part {
id 1
code "8581-Sign"
label "8581-Sign"
need +2
used +2
priority +1
turnAngleIncrement +180
ccAllowed +0
filler +0
area 141.761356753
positioningTime 10.369402427
cuttingTime 346.222969467
piercingTime 35.5976025504
positioningWay 1949.56
cuttingWay 9249.13
countPiercingNormal +75
countPiercingPuls +4
}
}
Plans {
Plan {
id 52313
label "Centric 20170117 16gaHRS 1"
filename "Centric 20170117 16gaHRS01"
border 0.5 0.5 0.5 0.5
cycleCount +1
waste 97.5516173272
positioningTime 11.9357066923
cuttingTime 345.629256802
piercingTime 35.5976025504
auxiliaryProcessTime 79.2405450926
positioningWay 1954.13
cuttingWay 9215.92
countPiercingNormal +75
countPiercingPuls +4
RawmaterialReference 52312
PartReferences {
PartReference {
id 1
layer 21
partId 1
insert -128.833464567 -97.2358267717
}
}
}
Plan {
id 52314
label "Centric 20170117 16gaHRS 2"
filename "Centric 20170117 16gaHRS02"
border 0.5 0.5 0.5 0.5
cycleCount +1
waste 97.5516173272
positioningTime 11.9357066923
cuttingTime 345.629256802
piercingTime 35.5976025504
auxiliaryProcessTime 79.2405450926
positioningWay 1954.13
cuttingWay 9215.92
countPiercingNormal +75
countPiercingPuls +4
RawmaterialReference 52312
PartReferences {
PartReference {
id 1
layer 21
partId 1
insert -128.833464567 -97.2358267717
}
}
}
}
首先,我想从code
部分提取Header
属性,并为每个filename
提取Plan
属性。
我可以遍历文件,记下花括号和我们当前处理的部分,但似乎必须有一个更简单的方法。如果它是JSON或XML数据,我可以很容易地解析它,但我对解析这种非标准格式的最简单方法感到茫然。
答案 0 :(得分:1)
没有简单的方法。
json和xml解析器完全相同,逐个字符地浏览文件并跟踪所有内容,只是其他人为您编写了该代码。
我看到了5个选项
我会尝试前两个选项...