Question

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 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>
Was it helpful?

Solution

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^/  "

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top