java数据的XML解析
我有以下xml文件:java数据的XML解析,java,xml,parsing,xml-parsing,Java,Xml,Parsing,Xml Parsing,我有以下xml文件: <?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)
<?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>
-3.87499999999*数学功率(x,4.0)+48.7499999993*数学功率(x,3.0)
-0.166667*数学功率(x,4.0)+2.166667*数学功率(x,3.0)
我想解析这两个方程,并将它们放入变量中进行进一步计算。我目前使用的方法是解析它们并将它们放入字符串中,但它不起作用,因为我需要使用公式执行计算
有没有更好的方法来解决这个问题?提前感谢。请尝试评估工具:
import bsh.EvalError;
import bsh.Interpreter;
public class BeanShellInterpreter {
public static void main(String[] args) throws EvalError {
Interpreter i = new Interpreter(); // Construct an interpreter
i.set("x", 5);
// Eval a statement and get the result
i.eval("eq1 = (-3.874999999999* Math.pow(x, 4.0) + 48.749999999993* Math.pow(x, 3.0))");
System.out.println( i.get("eq1") );
}
}
您可以使用JAXB将XML文件解组到具有这些字段的自定义对象 您的对象可能看起来像:
@XmlRootElement
public class Equations {
String eq1;
String eq2;
// create getters and setters as well
// and put the @XmlElement annotation on the setters
}
然后从方程对象中使用它们。(equations.getEq1()
例如)
下面是对JAXB的简单而快速的介绍:
关于方程的执行,一种方法是解析字符串,查看您拥有哪些指令和数字,并将它们放在堆栈上,然后在解析所有内容时,执行操作(您将在堆栈上拥有数字和操作)。也许这需要更多的工作,但这绝对是解决问题的一种有趣的方法。可能会提供一些见解。我认为Sysout必须是
System.out.println(I.get(“bar”)代码>我会调查的。这必须在一个移动应用程序上实现,所以我认为用尊重的公式创建两个变量更有意义,因为一个250k库对于一个应用程序来说相当大。