Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将XML对象转换为JAXB java obejct?_Java_Xml_Xml Parsing_Jaxb_Unmarshalling - Fatal编程技术网

如何将XML对象转换为JAXB java obejct?

如何将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

我的XML


这里有一个例子——你可以遵循-

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();
      }

    }
}