如何将XML对象转换为JAXB java obejct?
我的XML如何将XML对象转换为JAXB java obejct?,java,xml,xml-parsing,jaxb,unmarshalling,Java,Xml,Xml Parsing,Jaxb,Unmarshalling,我的XML 这里有一个例子——你可以遵循- quick.xml File file = new File("Test.xml"); JAXBContext jaxbContext = JAXBContext.newInstance(OadrPayload.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); OadrPayload stationSearch = (OadrPay
这里有一个例子——你可以遵循- quick.xml
File file = new File("Test.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(OadrPayload.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
OadrPayload stationSearch = (OadrPayload) jaxbUnmarshaller.unmarshal(file);
现在创建Quij.java、Question.java和Answer.java thrre文件
工作已经完成。也提供课程。
File file = new File("Test.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(OadrPayload.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
OadrPayload stationSearch = (OadrPayload) jaxbUnmarshaller.unmarshal(file);
<?xml version="1.0" encoding="UTF-8"?>
<quiz>
<question>
<questionText>Who is the author of this column?</questionText>
<answer correct='N'>Pat McClellan</answer>
<answer correct='N'>Darrel Plant</answer>
<answer correct='N'>Gary Rozensweig</answer>
<answer correct='Y'>Will Turnage</answer>
</question>
<question>
<questionText>Who is Director Online's Technical Editor?</questionText>
<answer correct='N'>Pat McClellan2</answer>
<answer correct='N'>Darrel Plant2</answer>
<answer correct='N'>Gary Rozensweig2</answer>
<answer correct='Y'>Will Turnage2</answer>
</question>
</quiz>
public class ReadXmlFileCode {
public static void main(String[] args) {
try {
//create object from xml
File file = new File("C:\\Qstn.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Quiz.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Quiz customer = (Quiz) jaxbUnmarshaller.unmarshal(file);
for(Question qs:customer.getQuestion())
{
System.out.println(qs.getQuestionText());
System.out.println(qs.getAnswer().get(0).getCorrect());
}
////create xml from object
File file1 = new File("C:\\file1.xml");
JAXBContext jaxbContext1 = JAXBContext.newInstance(Quiz.class);
Marshaller jaxbMarshaller = jaxbContext1.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(customer, file1);
jaxbMarshaller.marshal(customer, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}