List of usage examples for javax.xml.bind Marshaller JAXB_FRAGMENT
String JAXB_FRAGMENT
To view the source code for javax.xml.bind Marshaller JAXB_FRAGMENT.
Click Source Link
From source file:Main.java
public static void main(String[] args) throws Exception { XMLStreamWriter writer = XMLOutputFactory.newFactory().createXMLStreamWriter(System.out); writer.setDefaultNamespace("http://www.java2s.com"); JAXBContext jc = JAXBContext.newInstance(WorkSet.class); Marshaller m = jc.createMarshaller(); m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); writer.writeStartDocument();/*from w w w .ja va2 s . c o m*/ writer.writeStartElement("http://www.java2s.com", "Import"); writer.writeNamespace("", "http://www.java2s.com"); writer.writeStartElement("WorkSets"); m.marshal(new WorkSet(), writer); m.marshal(new WorkSet(), writer); writer.writeEndDocument(); writer.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { JAXBContext jxbc = JAXBContext.newInstance(Main.class); Marshaller marshaller = jxbc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); marshaller.marshal(new JAXBElement(new QName("root"), Main.class, new Main("test")), System.out); }
From source file:Main.java
public static String object2Xml(Object obj) throws JAXBException { JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); StringWriter writer = new StringWriter(); marshaller.marshal(new JAXBElement(new QName("xml"), obj.getClass(), obj), writer); return writer.toString(); }
From source file:Main.java
public static Marshaller getMarshaller(Class jaxbClass) throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance(jaxbClass); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); return marshaller; }
From source file:Main.java
public static String toXML(Object obj) { try {/*w w w.j a v a 2 s .c o m*/ JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); StringWriter writer = new StringWriter(); marshaller.marshal(obj, writer); return writer.toString(); } catch (JAXBException e) { e.printStackTrace(); return ""; } }
From source file:Main.java
public static String convertBean2Xml(Object obj) throws IOException { String result = null;//from ww w. j a va 2 s . c o m try { JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance(); XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(baos, (String) marshaller.getProperty(Marshaller.JAXB_ENCODING)); xmlStreamWriter.writeStartDocument((String) marshaller.getProperty(Marshaller.JAXB_ENCODING), "1.0"); marshaller.marshal(obj, xmlStreamWriter); xmlStreamWriter.writeEndDocument(); xmlStreamWriter.close(); result = baos.toString("UTF-8"); } catch (JAXBException e) { e.printStackTrace(); return null; } catch (XMLStreamException e) { e.printStackTrace(); return null; } catch (UnsupportedEncodingException e) { e.printStackTrace(); return null; } return result; }
From source file:Main.java
public static String bean2Xml(Object bean) { String xmlString = null;/*from w ww.j ava 2s . c o m*/ JAXBContext context; StringWriter writer; if (null == bean) return xmlString; try { context = JAXBContext.newInstance(bean.getClass()); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);// //m.setProperty(Marshaller.JAXB_ENCODING, "gb2312");// //m.setProperty(Marshaller.JAXB_ENCODING, "GBK");// m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// m.setProperty(Marshaller.JAXB_FRAGMENT, false);// writer = new StringWriter(); m.marshal(bean, writer); xmlString = writer.toString(); return xmlString; } catch (Exception e) { e.printStackTrace(); } return xmlString; }
From source file:Main.java
/** * //w w w . j ava2 s .co m * @param contextPath * @return Marshaller * @throws JAXBException */ public static Marshaller getMarshaller(final String contextPath) throws JAXBException { final JAXBContext jaxbContext = JAXBContext.newInstance(contextPath); final Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); return marshaller; }
From source file:Main.java
public static String bean2Xml(Object bean, String codetype) { String xmlString = null;/* w w w. jav a 2 s.c o m*/ JAXBContext context; StringWriter writer; if (null == bean) return xmlString; try { context = JAXBContext.newInstance(bean.getClass()); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);// // m.setProperty(Marshaller.JAXB_ENCODING, "gb2312");// // m.setProperty(Marshaller.JAXB_ENCODING, "GBK");// m.setProperty(Marshaller.JAXB_ENCODING, codetype);// m.setProperty(Marshaller.JAXB_FRAGMENT, false);// writer = new StringWriter(); m.marshal(bean, writer); xmlString = writer.toString(); return xmlString; } catch (Exception e) { e.printStackTrace(); } return xmlString; }
From source file:Main.java
public final String getMessage() throws Exception { Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty("jaxb.encoding", "ISO-8859-1"); jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(baos, (String) jaxbMarshaller.getProperty(Marshaller.JAXB_ENCODING)); xmlStreamWriter.writeStartDocument((String) jaxbMarshaller.getProperty(Marshaller.JAXB_ENCODING), "1.0"); jaxbMarshaller.marshal(this, xmlStreamWriter); xmlStreamWriter.writeEndDocument();//from w w w . ja v a 2 s . co m xmlStreamWriter.close(); return new String(baos.toByteArray()); }