I have the following xml file:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<eq1>-3.874999999999* Math.pow(x, 4.0) + 48.749999999993* Math.pow(x, 3.0)</eq1>
<eq2>-0.166666666667* Math.pow(x, 4.0) + 2.166666666667* Math.pow(x, 3.0)</eq2>
</root>
I would like to parse these 2 equations and place them in variables for further calculations. The method I’m using currently is parsing them and placing them in a string, but it won’t work as I need to perform the calculations using the equations.
Is there a better method I can use to work around this? Thanks in advance.
You can use JAXB to unmarshal the XML file to a custom Object with those fields.
Your object will probably look something like:
And then just use them from the Equations object. (
equations.getEq1()for example);Here’s a really simple and quick intro to JAXB: http://www.mkyong.com/java/jaxb-hello-world-example/
Regarding the execution of the equations, one way is to parse the string and see which instructions and numbers you have and put them on a stack and then when everything is parsed, execute the operations (you’ll have the numbers and operations on the stack). Maybe it’s a little more work but It’s definitely an interesting way to solve the problem.