Java XML动态解析器

Java XML动态解析器,java,xml,parsing,Java,Xml,Parsing,我在寻找一个简单的解决方案 我有一个xml文件: <properties> <property> <class>java.lang.String</class> <value>String value...</value> </property> <property> <class>java.lang.Boolean&

我在寻找一个简单的解决方案

我有一个xml文件:

<properties>
    <property>
        <class>java.lang.String</class>
        <value>String value...</value>
    </property>
    <property>
        <class>java.lang.Boolean</class>
        <value>true</value>
    </property>
    <!-- ... others java lang wrapper class -->
</properties>

java.lang.String
字符串值。。。
java.lang.Boolean
真的
我想做一个动态解析器

我知道我可以用org.w3c.dom.*和org.w3c.dom.Node.getTextContent()读取xml,我可以得到标记的值

Class<?> clazz = Class.forName(classTextContent);
// How to convert value to specific clazz?
// if/else (clazz)? Does not look a nice answer.
Class clazz=Class.forName(classTextContent);
//如何将值转换为特定类别?
//如果/否则(clazz)?这看起来不是个好答案。
有什么建议吗

[编辑]通过反射:

变量“clazz”是一个java.lang.Class,对吗? 如何将textContent值(字符串)转换为任何包装类型

Object objectValue = null;
Class<?> clazz = Class.forName(nodeClass.getTextContent());
String value = nodeValue.getTextContent();
if (clazz.equals(Boolean.class) { objectValue = Boolean.valueOf(value) }
objectobjectvalue=null;
Class clazz=Class.forName(nodeClass.getTextContent());
字符串值=nodeValue.getTextContent();
如果(clazz.equals(Boolean.class){objectValue=Boolean.valueOf(value)}
valueOf可能是我可以使用反射调用的通用方法。。。 整型、浮点型、布尔型、长型、双精度支持valueOf

另一种方法?

Object Object=YourClass.class.getConstructor(新类[]{}).newInstance();
Object object = YourClass.class.getConstructor(new Class<?>[]{}).newInstance();

// if you want to use a constructor, let's say, for YourClass(int name, int id)

Object object = YourClass.class.getConstructor(new Class<?>[]{int.class, int.class}).newInstance(desiredName, desiredId);
//如果您想为类使用构造函数(int-name,int-id) Object Object=YourClass.class.getConstructor(新类[]{int.class,int.class}).newInstance(desiredName,desiredId);
Object Object=YourClass.class.getConstructor(新类[]{}).newInstance();
//如果您想为类使用构造函数(int-name,int-id)
Object Object=YourClass.class.getConstructor(新类[]{int.class,int.class}).newInstance(desiredName,desiredId);

Reflection是您的答案:Reflection是您的答案:Emd4600请查看我编辑的问题。据我所知,感谢不是。您可以使用此选项,让用户指定要填充值的字段。Emd4600请查看我编辑的问题。感谢不是据我所知。您可以使用此选项,让用户指定f要用值填充的字段。