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