List of usage examples for javax.xml.bind JAXBContext newInstance
public static JAXBContext newInstance(Class<?>... classesToBeBound) throws JAXBException
From source file:Main.java
public static String bean2Xml(Object bean, String codetype) { String xmlString = null;/* ww w .java 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
@SuppressWarnings("unchecked") public static <T> T read(String input, Class<T> typeParameterClass) { T content = null;//from w w w . j a v a2 s . co m try { JAXBContext jaxbContext = JAXBContext.newInstance(typeParameterClass); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); //jaxbUnmarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); InputStream is = new ByteArrayInputStream(input.getBytes()); content = (T) jaxbUnmarshaller.unmarshal(is); } catch (JAXBException e) { e.printStackTrace(); } return content; }
From source file:Main.java
/** * Helper method to unmarshall a xml doc * @see http://docs.oracle.com/javase/tutorial/jaxb/intro/basic.html * http://jaxb.java.net/nonav/2.2.6/docs/ch03.html#unmarshalling * this uses <T> JAXBElement<T> unmarshal(Source source, Class<T> declaredType) throws JAXBException//w w w .j av a 2 s . c o m * */ /* public static <T> T unmarshall(Class<T> docClass, InputStream inputStream) throws JAXBException{ String packageName = docClass.getPackage().getName(); JAXBContext jc = JAXBContext.newInstance( packageName ); Unmarshaller u = jc.createUnmarshaller(); JAXBElement<T> root = u.unmarshal(new StreamSource(inputStream),docClass); return root.getValue(); }*/ public static <T> T unmarshall(Class<T> docClass, InputStream inputStream) throws JAXBException, ParserConfigurationException, SAXException { String packageName = docClass.getPackage().getName(); JAXBContext jc = JAXBContext.newInstance(packageName); SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setFeature("http://apache.org/xml/features/validation/schema", false); spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); InputSource inputSource = new InputSource(inputStream); SAXSource source = new SAXSource(xmlReader, inputSource); Unmarshaller u = jc.createUnmarshaller(); JAXBElement<T> root = u.unmarshal(source, docClass); return root.getValue(); }
From source file:Main.java
/** * Unmarshal XML data from XML DOM document using XSD string and return the resulting JAXB content tree * * @param dummyJAXBObject/*from w ww . j a v a 2 s . co m*/ * Dummy contect object for creating related JAXB context * @param doc * XML DOM document * @param strXSD * XSD * @return resulting JAXB content tree * @throws Exception * in error case */ public static Object doUnmarshallingFromDOMDocument(Object dummyJAXBObject, Document doc, String strXSD) throws Exception { if (dummyJAXBObject == null) { throw new RuntimeException("No dummy context objekt (null)!"); } if (doc == null) { throw new RuntimeException("No XML DOM document (null)!"); } if (strXSD == null) { throw new RuntimeException("No XSD document (null)!"); } Object unmarshalledObject = null; StringReader reader = null; try { JAXBContext jaxbCtx = JAXBContext.newInstance(dummyJAXBObject.getClass().getPackage().getName()); Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller(); reader = new StringReader(strXSD); javax.xml.validation.Schema schema = javax.xml.validation.SchemaFactory .newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(new StreamSource(reader)); unmarshaller.setSchema(schema); unmarshalledObject = unmarshaller.unmarshal(doc); } catch (Exception e) { // Logger.XMLEval.logState("Unmarshalling failed: " + e.getMessage(), LogLevel.Error); throw e; } finally { if (reader != null) { reader.close(); reader = null; } } return unmarshalledObject; }
From source file:Main.java
@SuppressWarnings("rawtypes") public static void marshal(Object object, Writer w) throws JAXBException { Class clazz = object.getClass(); JAXBContext context = s_contexts.get(clazz); if (context == null) { context = JAXBContext.newInstance(clazz); s_contexts.put(clazz, context);/*w w w. j a v a2 s . c om*/ } ValidationEventCollector valEventHndlr = new ValidationEventCollector(); Marshaller marshaller = context.createMarshaller(); marshaller.setSchema(null); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.setEventHandler(valEventHndlr); try { marshaller.marshal(object, w); } catch (Exception e) { if (e instanceof JAXBException) { throw (JAXBException) e; } else { throw new MarshalException(e.getMessage(), e); } } if (valEventHndlr.hasEvents()) { for (ValidationEvent valEvent : valEventHndlr.getEvents()) { if (valEvent.getSeverity() != ValidationEvent.WARNING) { // throw a new Marshall Exception if there is a parsing error throw new MarshalException(valEvent.getMessage(), valEvent.getLinkedException()); } } } }
From source file:Main.java
public Main() throws Exception { jaxbContext = JAXBContext.newInstance(Main.class); xmlOutputFactory = XMLOutputFactory.newFactory(); }
From source file:Main.java
/** * Unmarshal XML data from XML file path using XSD string and return the resulting JAXB content tree * /*w w w .jav a 2 s .c om*/ * @param dummyCtxObject * Dummy contect object for creating related JAXB context * @param strXMLFilePath * XML file path * @param strXSD * XSD * @return resulting JAXB content tree * @throws Exception * in error case */ public static Object doUnmarshallingFromXMLFile(Object dummyCtxObject, String strXMLFilePath, String strXSD) throws Exception { if (dummyCtxObject == null) { throw new RuntimeException("No dummy context object (null)!"); } if (strXMLFilePath == null) { throw new RuntimeException("No XML file path (null)!"); } if (strXSD == null) { throw new RuntimeException("No XSD (null)!"); } Object unmarshalledObject = null; try { JAXBContext jaxbCtx = JAXBContext.newInstance(dummyCtxObject.getClass().getPackage().getName()); Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller(); // unmarshaller.setValidating(true); /* javax.xml.validation.Schema schema = javax.xml.validation.SchemaFactory.newInstance( javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema( new java.io.File(m_strXSDFilePath)); */ StringReader reader = null; FileInputStream fis = null; try { reader = new StringReader(strXSD); javax.xml.validation.Schema schema = javax.xml.validation.SchemaFactory .newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI) .newSchema(new StreamSource(reader)); unmarshaller.setSchema(schema); fis = new FileInputStream(strXMLFilePath); unmarshalledObject = unmarshaller.unmarshal(fis); } finally { if (fis != null) { fis.close(); fis = null; } if (reader != null) { reader.close(); reader = null; } } // } catch (JAXBException e) { // //m_logger.error(e); // throw new OrderException(e); } catch (Exception e) { // Logger.XMLEval.logState("Unmarshalling failed: " + e.getMessage(), LogLevel.Error); throw e; } return unmarshalledObject; }
From source file:edu.duke.cabig.c3pr.rules.common.XMLUtil.java
public static Object unmarshal(String xml) throws RuleException { try {//from w w w .j a va2 s . com Unmarshaller unmarshaller = JAXBContext.newInstance("edu.duke.cabig.c3pr.rules.brxml") .createUnmarshaller(); log.debug("reading the rule:" + xml); return unmarshaller.unmarshal(new StringReader(xml)); } catch (JAXBException e) { throw new RuleException(e.getMessage(), e); } }
From source file:Main.java
public static Object parseRequestObjectFromSoap(String soapXml) throws Exception { Object result = null;//w w w .j a v a 2s. co m if (soapXml != null && soapXml.trim().length() > 0) { DocumentBuilderFactory xmlFact = DocumentBuilderFactory.newInstance(); xmlFact.setNamespaceAware(true); DocumentBuilder builder = xmlFact.newDocumentBuilder(); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); StringReader xsr = new StringReader(soapXml); InputSource is = new InputSource(xsr); Document doc = builder.parse(is); //Node requestNode = (Node) xpath.evaluate("/soap:Envelope/soap:Body/*", doc, XPathConstants.NODE); Node requestNode = (Node) xpath.evaluate("/*[local-name()='Envelope']/*[local-name()='Body']/*", doc, XPathConstants.NODE); JAXBContext ctx = JAXBContext.newInstance("xo.xoi.orderentry.ebonding"); Unmarshaller unmarshaller = ctx.createUnmarshaller(); result = unmarshaller.unmarshal(requestNode); } return result; }
From source file:Main.java
/** * * @param obj/* ww w. jav a 2 s. c o m*/ * @return * @throws JAXBException */ public static String serializeToString(Object obj) throws JAXBException { java.io.StringWriter sw = new StringWriter(); final Marshaller m = JAXBContext.newInstance(obj.getClass()).createMarshaller(); m.setProperty(JAXB_FRAGMENT, TRUE); m.setProperty(JAXB_FORMATTED_OUTPUT, TRUE); m.marshal(obj, sw); return sw.toString(); }