Creates a new instance of {@link Marshaller}. The {@link Marshaller} can be usedto convert JAXB objects into XML data.
Note: Marshallers are reusable, but not reentrant (thread safe).
6364656667686970717273
// Unmarshaller unmarshaller = new UnmarshallerImpl(Collections.<JAXBMarshaller>singleton(EjbJarJaxB.INSTANCE)); // Marshaller marshaller = new MarshallerImpl(Collections.<JAXBMarshaller>singleton(EjbJarJaxB.INSTANCE)); JAXBContext ctx = JAXBContextFactory.newInstance(EjbJar.class); Unmarshaller unmarshaller = ctx.createUnmarshaller(); Marshaller marshaller = ctx.createMarshaller(); NamespaceFilter xmlFilter = new NamespaceFilter(parser.getXMLReader()); xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler()); unmarshaller.setEventHandler(new TestValidationEventHandler());
147148149150151152153154155156157
SAXSource source = new SAXSource(xmlFilter, new InputSource(new ByteArrayInputStream(expected.getBytes()))); Object object = unmarshaller.unmarshal(source); Marshaller marshaller = ctx.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); ByteArrayOutputStream baos = new ByteArrayOutputStream(); marshaller.marshal(object, baos);
6263646566676869707172
} protected final Marshaller getMarshaller(Class<?> type, MediaType mediaType) throws JAXBException { JAXBContext context = getContext(type, mediaType); return context.createMarshaller(); } protected boolean isSupportedMediaType(MediaType mediaType) { return MediaTypeUtils.isXmlType(mediaType); }
126127128129130131132133134135136
try { return parentTypeConverter.convertTo(type, source); } catch (NoTypeConversionAvailableException e) { // lets try a stream StringWriter buffer = new StringWriter(); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, isPrettyPrint() ? Boolean.TRUE : Boolean.FALSE); marshaller.marshal(value, buffer); return parentTypeConverter.convertTo(type, buffer.toString()); } }
6566676869707172737475
JAXBElement element = (JAXBElement) object; type = element.getValue().getClass(); } JAXBContext ctx = JAXBContext.newInstance(type); Marshaller marshaller = ctx.createMarshaller(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); marshaller.marshal(object, baos); String xml = new String(baos.toByteArray());
199200201202203204205206207208209
e.setTitle(b.getName()); e.setId(Long.toString(b.getId())); StringWriter writer = new StringWriter(); jc.createMarshaller().marshal(b, writer); Content ct = factory.newContent(Content.Type.XML); ct.setValue(writer.toString()); e.setContentElement(ct); return e;
1819202122232425262728
CreditCard07 creditCard = new CreditCard07("1234", "12/09", 6398, "Visa"); StringWriter writer = new StringWriter(); JAXBContext context = JAXBContext.newInstance(CreditCard07.class); Marshaller m = context.createMarshaller(); m.marshal(creditCard, writer); System.out.println(writer.toString()); } }
5657585960616263646566
CreditCard11 creditCard = new CreditCard11("12345678", "10/14", 566, "Visa"); StringWriter writer = new StringWriter(); JAXBContext context = JAXBContext.newInstance(CreditCard11.class); Marshaller m = context.createMarshaller(); m.marshal(creditCard, writer); System.out.println(writer); assertEquals(creditCardXML, writer.toString());
102103104105106107108109110111112
} // create a JAXBContext for the PMML class JAXBContext ctx = null; try { ctx = JAXBContext.newInstance(PMML.class); Marshaller marshaller = ctx.createMarshaller(); // the property JAXB_FORMATTED_OUTPUT specifies whether or not the // marshalled XML data is formatted with linefeeds and indentation marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // marshal the data in the Java content tree StringWriter stringWriter = new StringWriter();
151152153154155156157158159160161
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException { try { JAXBContext jaxbctx = getJAXBContext(type, mediaType); Marshaller m = jaxbctx.createMarshaller(); // Must respect application specified character set. String charset = mediaType.getParameters().get("charset"); if (charset != null) m.setProperty(Marshaller.JAXB_ENCODING, charset);