XML. (was: Re: [ODE] outputting objects)
Martin C. Martin
martin at metahuman.org
Tue Feb 4 10:36:01 2003
This is a multi-part message in MIME format.
--------------693E9A00AE7D14C2F772434B
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Anselm Hook wrote:
>
> I'd still argue that the point is still not the xml itself - it is that
> you need some tool to map between *any* notation and C objects. lets say
> that you have an xml file:
>
> <ode:body>
> <position>0 0 0</position>
> </ode:body>
>
> how do you pluck out and apply position to the ode object without having
> to hand write code? do you write code like this:
>
> for( all xml nodes ) {
> if( mode is body )
> if( node is position )
> body->position = parse_field
> }
>
> clearly this would be a large mass of messy code.
Yeah, what you really need is a parser generator, like yacc, but that
generates top down parsers. Recursive descent parsers would be ideal.
I just use yacc and a lot of copying, and a lex file I got off the web.
I've included it here.
- Martin
--------------693E9A00AE7D14C2F772434B
Content-Type: application/x-unknown-content-type-LEXFile;
name="xml.lex"
Content-Transfer-Encoding: base64
Content-Disposition: inline;
filename="xml.lex"
LyogVG8gY29tcGlsZToNCiAgIA0KICAgZmxleCB4bWwubGV4DQogICBtdiBsZXgueXkuYyBs
ZXgueXkuY3BwDQogICBnKysgbGV4Lnl5LmNwcA0KICAgKi8NCg0KDQovKiBSRVgvTGV4IDEu
MCANCiAgIFJvYmVydCBELiBDYW1lcm9uICJSRVg6IFhNTCBTaGFsbG93IFBhcnNpbmcgd2l0
aCBSZWd1bGFyIEV4cHJlc3Npb25zIiwNCiAgIFRlY2huaWNhbCBSZXBvcnQgVFIgMTk5OC0x
NywgU2Nob29sIG9mIENvbXB1dGluZyBTY2llbmNlLCBTaW1vbiBGcmFzZXIgDQogICBVbml2
ZXJzaXR5LCBOb3ZlbWJlciwgMTk5OC4NCiAgIENvcHlyaWdodCAoYykgMTk5OCwgUm9iZXJ0
IEQuIENhbWVyb24uIA0KICAgVGhlIGZvbGxvd2luZyBjb2RlIG1heSBiZSBmcmVlbHkgdXNl
ZCBhbmQgZGlzdHJpYnV0ZWQgcHJvdmlkZW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