0

I have a situation where I need to pull in different statements from the xml, based on values for a set of variables. e.g.

if a>10 and b<20 then string="ANCD"
if a>10 and (d+e)<0 then string="DEFG"
else string="XYZ"

Rather than build these conditions in code, is it possible to cleanly have them in xml in some structure like this, and code to parse the condition strings and get string value?

<conditionalstring>
<condition>"a>10 and b<20"</condition>
<string>"ANCD"</string>
</conditionalstring>
<conditionalstring>
<condition>"a>10 and (d+e)<0"</condition>
<string>"DEFG"</string>
</conditionalstring>
<conditionalstring>
<condition>default</condition>
<string>"XYZ"</string>
</conditionalstring>
YOU
  • 120,166
  • 34
  • 186
  • 219
RG1967
  • 378
  • 1
  • 15

1 Answers1

1

You can certainly store your logic as XML. The question is, how to get it out and evaluate the conditions? A lot will depend here on what language and tools you're using. If you have an expression parser available already (e.g. if you are working in a language with eval() such as Perl, Python, Javascript, and many others), you might be able to leverage that. If you're working in .Net, you might be able to use the windows workflow rules engine (http://msdn.microsoft.com/en-us/library/dd349785.aspx). Otherwise, you'll need to borrow some other language's parser (e.g. in .Net or Java you could find a Scheme interpreter or a Python interpreter or something), or you'll have to write a parser of your own. If you've not written a parser before, it will definitely take some time, so you might want to consider leveraging an existing one.

Bryn Keller
  • 969
  • 6
  • 14
  • I am working on Adobe flex and eval seems to be deprecated. would you suggest writing a expression parser- how challenging would that be? – RG1967 Feb 27 '11 at 15:28
  • [This SO post](http://stackoverflow.com/questions/2554519/javascript-parser-in-javascript) points out that there's a complete javascript parser written in javascript in [jslint](http://www.jslint.com/fulljslint.js), perhaps you could use that as a starting point or example. You're only interested in a small part of the whole javascript syntax, but at least it would give you some good structure - Crockford definitely knows his Javascript. – Bryn Keller Feb 27 '11 at 17:36