Search in sources :

Example 1 with Tutorials

use of com.baeldung.xml.binding.Tutorials in project tutorials by eugenp.

the class JaxbParser method getFullDocument.

public Tutorials getFullDocument() {
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(Tutorials.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        Tutorials tutorials = (Tutorials) jaxbUnmarshaller.unmarshal(this.getFile());
        return tutorials;
    } catch (JAXBException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : Tutorials(com.baeldung.xml.binding.Tutorials) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 2 with Tutorials

use of com.baeldung.xml.binding.Tutorials in project tutorials by eugenp.

the class JaxbParser method createNewDocument.

public void createNewDocument() {
    Tutorials tutorials = new Tutorials();
    tutorials.setTutorial(new ArrayList<Tutorial>());
    Tutorial tut = new Tutorial();
    tut.setTutId("01");
    tut.setType("XML");
    tut.setTitle("XML with Jaxb");
    tut.setDescription("XML Binding with Jaxb");
    tut.setDate("04/02/2015");
    tut.setAuthor("Jaxb author");
    tutorials.getTutorial().add(tut);
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(Tutorials.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.marshal(tutorials, file);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
}
Also used : Tutorial(com.baeldung.xml.binding.Tutorial) Marshaller(javax.xml.bind.Marshaller) Tutorials(com.baeldung.xml.binding.Tutorials) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext)

Example 3 with Tutorials

use of com.baeldung.xml.binding.Tutorials in project tutorials by eugenp.

the class JaxbParserUnitTest method createNewDocumentTest.

@Test
public void createNewDocumentTest() {
    File newFile = new File("src/test/resources/example_jaxb_new.xml");
    parser = new JaxbParser(newFile);
    parser.createNewDocument();
    assertTrue(newFile.exists());
    Tutorials tutorials = parser.getFullDocument();
    assertNotNull(tutorials);
    assertTrue(tutorials.getTutorial().size() == 1);
    assertTrue(tutorials.getTutorial().get(0).getTitle().equalsIgnoreCase("XML with Jaxb"));
}
Also used : Tutorials(com.baeldung.xml.binding.Tutorials) File(java.io.File) Test(org.junit.Test)

Example 4 with Tutorials

use of com.baeldung.xml.binding.Tutorials in project tutorials by eugenp.

the class JaxbParserUnitTest method getFullDocumentTest.

@Test
public void getFullDocumentTest() {
    parser = new JaxbParser(new File(fileName));
    Tutorials tutorials = parser.getFullDocument();
    assertNotNull(tutorials);
    assertTrue(tutorials.getTutorial().size() == 4);
    assertTrue(tutorials.getTutorial().get(0).getType().equalsIgnoreCase("java"));
}
Also used : Tutorials(com.baeldung.xml.binding.Tutorials) File(java.io.File) Test(org.junit.Test)

Aggregations

Tutorials (com.baeldung.xml.binding.Tutorials)4 File (java.io.File)2 JAXBContext (javax.xml.bind.JAXBContext)2 JAXBException (javax.xml.bind.JAXBException)2 Test (org.junit.Test)2 Tutorial (com.baeldung.xml.binding.Tutorial)1 Marshaller (javax.xml.bind.Marshaller)1 Unmarshaller (javax.xml.bind.Unmarshaller)1