我需要将XML字符串解析为Object。请在下面找到类和XML字符串详细信息
国家/地区类
public class Country {
private String Country_Name;
private String Country_Capital;
private String Country_Foundation_Date;
private String Country_Continent;
private String Country_Population;
public String getCountry_Name() {
return Country_Name;
}
public void setCountry_Name(String country_Name) {
Country_Name = country_Name;
}
public String getCountry_Capital() {
return Country_Capital;
}
public void setCountry_Capital(String country_Capital) {
Country_Capital = country_Capital;
}
public String getCountry_Foundation_Date() {
return Country_Foundation_Date;
}
public void setCountry_Foundation_Date(String country_Foundation_Date) {
Country_Foundation_Date = country_Foundation_Date;
}
public String getCountry_Continent() {
return Country_Continent;
}
public void setCountry_Continent(String country_Continent) {
Country_Continent = country_Continent;
}
public String getCountry_Population() {
return Country_Population;
}
public void setCountry_Population(String country_Population) {
Country_Population = country_Population;
}
}
解析代码实施
import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
public class ParseXMLStringtoObject {
public static String getXMLString() {
StringBuilder returnString = new StringBuilder();
returnString.append("<output xmlprop = \"somevalue\"> ");
returnString.append("<Country> ");
returnString.append("<Country_Name equals=\"Spain\" type=\"1\" /> ");
returnString.append("<Country_Capital equals = \"Madrid\" type=\"1\" /> ");
returnString.append("<Country_Foundation_Date equals=\"1469-10-19\" type=\"3\" /> ");
returnString.append("<Country_Continent equals=\"Europe\" type = \"1\" /> ");
returnString.append("<Country_Population equals=\"45\" type = \"2\" /> ");
returnString.append("</Country> ");
return returnString.toString();
}
public static Object getObjectFromStringXml(Class<Country> country)
throws IllegalAccessException, InstantiationException {
Object obj = getObject(country.newInstance());
try {
StringReader reader = new StringReader(getXMLString());
JAXBContext context = JAXBContext.newInstance(country);
Unmarshaller jaxbUnmarshaller = context.createUnmarshaller();
JAXBElement<?> root = jaxbUnmarshaller.unmarshal(new StreamSource(reader), country);
obj = root.getValue();
} catch (Exception e) {
System.out.printf("Exception occoured while unmarshalling xml String to " + obj.getClass() +" Object", e);
}
return obj;
}
private static Object getObject(Object obj) {
if (obj instanceof Country) {
obj = (Country) obj;
}
return obj;
}
public static void main(String[] args) {
Country country = new Country();
//Class<Country> countryObj = new Class<Country>(); // I don't know how to convert my Country object
//or don't know the input value to the getObjectFromStringXml method
ParseXMLStringtoObject parseXML = new ParseXMLStringtoObject();
try{
//This is the place I have the doubt to call the parse method, regarding input
Object obj = parseXML.getObjectFromStringXml(Class<Country> countryObj);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
其中type属性指示equals属性中存在的值的类型。例如,1表示String,2表示int,3表示列表。
任何人都可以让我知道将上述xml字符串映射到Java Object的解决方案。
答案 0 :(得分:0)
您可以尝试使用JAXB对java对象进行解组:如下所示:
public static Object getObjectFromStringXml(String xml, Class<Country> country)
throws IllegalAccessException, InstantiationException {
Object obj = getObject(country.newInstance());
try {
StringReader reader = new StringReader(xml);
JAXBContext context = JAXBContext.newInstance(country);
Unmarshaller jaxbUnmarshaller = context.createUnmarshaller();
JAXBElement<?> root = jaxbUnmarshaller.unmarshal(new StreamSource(
reader), country);
obj = root.getValue();
} catch (Exception e) {
log.info("Exception occoured while unmarshalling xml String to " + obj.getClass() +" Object", e);
}
return obj;
}
private static Object getObject(Object obj) {
if (obj instanceof Country) {
obj = (Country) obj;
}
return obj;
}