List of usage examples for javax.xml.bind JAXBContext createMarshaller
public abstract Marshaller createMarshaller() throws JAXBException;
From source file:org.springframework.hateoas.VndErrorsMarshallingTests.java
@SuppressWarnings("deprecation") @Before/* ww w.j ava 2 s .com*/ public void setUp() throws Exception { jackson1Mapper = new ObjectMapper(); jackson1Mapper.registerModule(new Jackson1HalModule()); jackson1Mapper.configure(Feature.INDENT_OUTPUT, true); //jackson1Mapper.getSerializationConfig().withSerializationInclusion(JsonSerialize.Inclusion.NON_EMPTY); jackson1Mapper.configure(SerializationConfig.Feature.WRITE_NULL_PROPERTIES, false); jackson2Mapper = new com.fasterxml.jackson.databind.ObjectMapper(); jackson2Mapper.registerModule(new Jackson2HalModule()); jackson2Mapper.configure(SerializationFeature.INDENT_OUTPUT, true); jackson2Mapper.setSerializationInclusion(Include.NON_NULL); JAXBContext context = JAXBContext.newInstance(VndErrors.class); marshaller = context.createMarshaller(); VndError error = new VndError("42", "Validation failed!", // new Link("http://...", "help"), new Link("http://...", "describes")); errors = new VndErrors(error, error, error); }
From source file:actions.AddStudent.java
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ActionForward findForward = mapping.findForward("main"); StudentForm formStudent = (StudentForm) request.getAttribute("studentForm"); Student newStudent = new Student(); BeanUtils.copyProperties(newStudent, formStudent); System.out.println("newStudent.firstName=" + newStudent.getFirstName()); System.out.println("newStudent.lastName=" + newStudent.getLastName()); System.out.println("newStudent.studentId=" + newStudent.getStudentId()); System.out.println("newStudent.dob=" + newStudent.getDob()); //Student.fileWrite(null, newStudent.fileOutputString()); boolean successAdd = newStudent.saveStudent(); Student.getStudents().put(newStudent.getStudentId(), newStudent); ActionMessages messages = new ActionMessages(); if (successAdd) { messages.add("message1", (new ActionMessage("label.student.added.successfully"))); } else {/*from w ww . j a v a 2s . c o m*/ messages.add("error", (new ActionMessage("label.student.added.error"))); } saveMessages(request, messages); //********************************************************************** //********************************************************************** //********************************************************************** //http://www.vogella.com/tutorials/JAXB/article.html // create JAXB context and instantiate marshaller JAXBContext context = JAXBContext.newInstance(Student.class); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); // Write to System.out StringWriter sw = new StringWriter(); m.marshal(newStudent, sw); String xmlStudent = sw.toString(); System.out.println("xmlEncodedStudent=" + xmlStudent); //********************************************************************** //********************************************************************** //Unmarshall back for testing Unmarshaller um = context.createUnmarshaller(); Student backStudent = (Student) um.unmarshal(new StringReader(xmlStudent)); System.out.println("Student back from xml:" + backStudent.toString()); //********************************************************************** //********************************************************************** //********************************************************************** return findForward; }
From source file:edu.htwm.vsp.phone.services.XmlMappingTest.java
@Before public void prepareXmlMarshalling() throws JAXBException { /*//from w w w .j a v a 2 s. c o m * Code Duplizierung vermeiden * (DRY -> http://de.wikipedia.org/wiki/Don%E2%80%99t_repeat_yourself) und * aufwndige, immer wieder verwendete Objekte nur einmal erzeugen * */ JAXBContext context = JAXBContext.newInstance(PhoneUser.class, PhoneNumber.class); marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); unmarshaller = context.createUnmarshaller(); }
From source file:eu.over9000.skadi.io.PersistenceHandler.java
public PersistenceHandler() { try {// w w w.j a v a2 s . c om final JAXBContext context = JAXBContext.newInstance(StateContainer.class); this.marshaller = context.createMarshaller(); this.unmarshaller = context.createUnmarshaller(); this.marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); } catch (final JAXBException e) { LOGGER.error("exception construction persistence handler", e); } }
From source file:ait.ffma.service.preservation.riskmanagement.api.riskanalysis.risk.RiskUtils.java
/** * This method writes java object to XML file * /*from w ww . j a va 2s . c o m*/ * @param object * The Java object that should be converted to XML format * @param filePath * The path to the place where created XML file should be stored */ public static void storeRiskDataInXML(Object object, String filePath) { try { FileWriter fileWriter = new FileWriter(filePath); try { JAXBContext context = JAXBContext.newInstance(object.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.marshal(object, fileWriter); fileWriter.close(); } finally { fileWriter.close(); } } catch (JAXBException e) { LOG.log(Level.SEVERE, e.getMessage()); } catch (IOException e) { LOG.log(Level.SEVERE, e.getMessage()); } }
From source file:com.castlemock.web.basis.support.FileRepositorySupport.java
public <T> void save(T type, String filename) { Writer writer = null;/*from w ww. j a v a 2 s .co m*/ try { JAXBContext context = JAXBContext.newInstance(type.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); writer = new FileWriter(filename); marshaller.marshal(type, writer); } catch (JAXBException e) { LOGGER.error("Unable to parse file: " + filename, e); throw new IllegalStateException("Unable to parse the following file: " + filename); } catch (IOException e) { LOGGER.error("Unable to read file: " + filename, e); throw new IllegalStateException("Unable to read the following file: " + filename); } finally { if (writer != null) { try { writer.close(); } catch (IOException e) { LOGGER.error("Unable to close file writer for type " + type.getClass().getSimpleName(), e); } } } }
From source file:org.javelin.sws.ext.bind.SweJaxbContextFactoryTest.java
@Test public void emptySetOfClassesForJaxbRi() throws Exception { JAXBContext ctx = JAXBContext.newInstance(); System.out.println(ctx.toString()); ctx.createMarshaller().marshal( new JAXBElement<String>(new QName("urn:test", "str"), String.class, "content"), System.out); }
From source file:demo.wseventing.CreateSubscriptionServlet.java
public String convertJAXBElementToStringAndEscapeHTML(Object o) throws JAXBException { JAXBContext jc = JAXBContext.newInstance(Subscribe.class.getPackage().getName()); Marshaller m = jc.createMarshaller(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); m.marshal(o, baos);/*from w w w .j a v a 2 s .c om*/ String unescaped = baos.toString(); return StringEscapeUtils.escapeHtml4(unescaped); }
From source file:org.springframework.hateoas.VndErrorsMarshallingTest.java
@Before public void setUp() throws Exception { jackson2Mapper = new com.fasterxml.jackson.databind.ObjectMapper(); jackson2Mapper.registerModule(new Jackson2HalModule()); jackson2Mapper.setHandlerInstantiator(new Jackson2HalModule.HalHandlerInstantiator(relProvider, null)); jackson2Mapper.configure(SerializationFeature.INDENT_OUTPUT, true); JAXBContext context = JAXBContext.newInstance(VndErrors.class); marshaller = context.createMarshaller(); unmarshaller = context.createUnmarshaller(); VndError error = new VndError("42", "Validation failed!", // new Link("http://...", "describes"), new Link("http://...", "help")); errors = new VndErrors(error, error, error); }
From source file:com.quangphuong.crawler.util.XMLUtil.java
public <T> void marshallUtil(String xmlPath, T entityClass) { try {/*from w w w. ja va 2 s. co m*/ JAXBContext cxt = JAXBContext.newInstance(entityClass.getClass()); Marshaller marshaller = cxt.createMarshaller(); marshaller.setProperty(marshaller.JAXB_FORMATTED_OUTPUT, true); File file = new File(rootPath, xmlPath); System.out.println("Path: " + file.getAbsolutePath()); marshaller.marshal(entityClass, file); } catch (Exception e) { e.printStackTrace(); } }