Search in sources :

Example 1 with Tutorial

use of com.baeldung.xml.binding.Tutorial 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 2 with Tutorial

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

the class StaxParser method getAllTutorial.

public List<Tutorial> getAllTutorial() {
    boolean bTitle = false;
    boolean bDescription = false;
    boolean bDate = false;
    boolean bAuthor = false;
    List<Tutorial> tutorials = new ArrayList<Tutorial>();
    try {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLEventReader eventReader = factory.createXMLEventReader(new FileReader(this.getFile()));
        Tutorial current = null;
        while (eventReader.hasNext()) {
            XMLEvent event = eventReader.nextEvent();
            switch(event.getEventType()) {
                case XMLStreamConstants.START_ELEMENT:
                    StartElement startElement = event.asStartElement();
                    String qName = startElement.getName().getLocalPart();
                    if (qName.equalsIgnoreCase("tutorial")) {
                        current = new Tutorial();
                        Iterator<Attribute> attributes = startElement.getAttributes();
                        while (attributes.hasNext()) {
                            Attribute currentAt = attributes.next();
                            if (currentAt.getName().toString().equalsIgnoreCase("tutId")) {
                                current.setTutId(currentAt.getValue());
                            } else if (currentAt.getName().toString().equalsIgnoreCase("type")) {
                                current.setType(currentAt.getValue());
                            }
                        }
                    } else if (qName.equalsIgnoreCase("title")) {
                        bTitle = true;
                    } else if (qName.equalsIgnoreCase("description")) {
                        bDescription = true;
                    } else if (qName.equalsIgnoreCase("date")) {
                        bDate = true;
                    } else if (qName.equalsIgnoreCase("author")) {
                        bAuthor = true;
                    }
                    break;
                case XMLStreamConstants.CHARACTERS:
                    Characters characters = event.asCharacters();
                    if (bTitle) {
                        if (current != null) {
                            current.setTitle(characters.getData());
                        }
                        bTitle = false;
                    }
                    if (bDescription) {
                        if (current != null) {
                            current.setDescription(characters.getData());
                        }
                        bDescription = false;
                    }
                    if (bDate) {
                        if (current != null) {
                            current.setDate(characters.getData());
                        }
                        bDate = false;
                    }
                    if (bAuthor) {
                        if (current != null) {
                            current.setAuthor(characters.getData());
                        }
                        bAuthor = false;
                    }
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    EndElement endElement = event.asEndElement();
                    if (endElement.getName().getLocalPart().equalsIgnoreCase("tutorial")) {
                        if (current != null) {
                            tutorials.add(current);
                        }
                    }
                    break;
            }
        }
    } catch (FileNotFoundException | XMLStreamException e) {
        e.printStackTrace();
    }
    return tutorials;
}
Also used : Tutorial(com.baeldung.xml.binding.Tutorial) Attribute(javax.xml.stream.events.Attribute) EndElement(javax.xml.stream.events.EndElement) Characters(javax.xml.stream.events.Characters) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) StartElement(javax.xml.stream.events.StartElement) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) FileReader(java.io.FileReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Aggregations

Tutorial (com.baeldung.xml.binding.Tutorial)2 Tutorials (com.baeldung.xml.binding.Tutorials)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 ArrayList (java.util.ArrayList)1 JAXBContext (javax.xml.bind.JAXBContext)1 JAXBException (javax.xml.bind.JAXBException)1 Marshaller (javax.xml.bind.Marshaller)1 XMLEventReader (javax.xml.stream.XMLEventReader)1 XMLInputFactory (javax.xml.stream.XMLInputFactory)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 Attribute (javax.xml.stream.events.Attribute)1 Characters (javax.xml.stream.events.Characters)1 EndElement (javax.xml.stream.events.EndElement)1 StartElement (javax.xml.stream.events.StartElement)1 XMLEvent (javax.xml.stream.events.XMLEvent)1