public class CreateObj {public Object createObj(){String objName = "Person";Map fieldMap = new HashMap();fieldMap.put("name", "java.lang.String");fieldMap.put("age", "int");return null;}}上面这个类中的createObj方法能根据 objName和filedMap中的值创建一个和public class Person {private String name;private int age;}Person person1 = new Person();相同的对象?
10 回答
饮歌长啸
TA贡献1951条经验 获得超3个赞
class Persion{
String name;
int age;
@XmlAttribute
public String getName() {return name};
public void setName(...){...}
@XmlAttribute
public String getAge(){return age};
public void setAge(...){...};
}
String xml = "<?xml version=\"1.0\">";
ByteArrayInputStream inStream = new ByteArrayInputStream(xml.getBytes());
JAXBContext jc = JAXBContext.newInstance(Persion.class);
StreamSource xml = new StreamSource(inputStream);
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement je1 = unmarshaller.unmarshal(xml, pclass);
Persion persion = je1.getValue();
通过jaxb解析xml字符串生成对象
添加回答
举报
0/150
提交
取消