开发者

How to parse a hierarchical structure in Rebol?

开发者 https://www.devze.com 2022-12-22 19:44 出处:网络
I have reread chapter http://www.rebol.com/docs/core23/rebolcore-15.html on any and parse but can\'t achieve to parse this kind of 开发者_StackOverflow社区hiearchical structure: is it possible ?

I have reread chapter http://www.rebol.com/docs/core23/rebolcore-15.html on any and parse but can't achieve to parse this kind of 开发者_StackOverflow社区hiearchical structure: is it possible ?

<Multipage>
<tab id=1>
  <box id=1>
  </box>
</tab>
<tab id=2>
  <box id=2>
  Hello
  </box>
</tab>
<tab>
</tab>
<tab>
</tab>
</Multipage>


Gavin MacKenzie's xml-parse script at http://www.rebol.org/view-script.r?script=xml-parse.r1 will parse most XML data. As it is a generalised solution it is understandably more complex than a set of parse rules for a specific XML file.


Yes it's possible and not very hard :

data: {...}

ws-chars: charset " ^/^M^-"
ws: [any ws-chars]

rule: [
    ws <Multipage> any [
        ws "<tab" opt [ws "id=" copy id to ">" (print ["tab id:" id])] ">" any [
            ws "<box" opt [ws "id=" copy value to ">" (print ["box id:" id])] ">"
                opt [copy text to "<" (if text [?? text])]
            </box>
        ]
        ws </tab>
    ]
    ws </Multipage> ws
]

parse/all data rule

Running this code, you'll get as output:

tab id: "1"
box id: "1"
text: "^/  "
tab id: "2"
box id: "2"
text: "^/  Hello^/  "
0

精彩评论

暂无评论...
验证码 换一张
取 消