JAXBContext jc = JAXBContext.newInstance(String.class, Bar.class); JAXBIntrospector introspector = jc.createJAXBIntrospector(); Marshaller marshaller = jc.createMarshaller(); if(null == introspector.getElementName(value)) { JAXBElement jaxbElement = new JAXBElement(new QName("ROOT"), Object.class, value); marshaller.marshal(jaxbElement, System.out); } else { marshaller.marshal(value, System.out);
public static Object unmarshalString(String str, JAXBContext jc, Class declaredType) throws JAXBException { Unmarshaller u = jc.createUnmarshaller(); u.setEventHandler(new org.docx4j.jaxb.JaxbValidationEventHandler()); Object o = u.unmarshal( new javax.xml.transform.stream.StreamSource(new java.io.StringReader(str)), declaredType); if (o instanceof JAXBElement) { return ((JAXBElement)o).getValue(); } else { return o; } }
public static String JAXBElementDebug(javax.xml.bind.JAXBElement o) { String prefix = null; if (o.getName().getNamespaceURI()!=null) { try { prefix = NamespacePrefixMapperUtils.getPreferredPrefix(o.getName().getNamespaceURI(), null, false); } catch (JAXBException e) { e.printStackTrace(); } } if (prefix!=null) { return prefix + ':' + o.getName().getLocalPart() + " is a javax.xml.bind.JAXBElement; it has declared type " + o.getDeclaredType().getName(); } else { return o.getName() + " is a javax.xml.bind.JAXBElement; it has declared type " + o.getDeclaredType().getName(); } }
@XmlTransient public org.docx4j.dml.picture.Pic getPic() { for (Object o : getAny() ) { if (o instanceof Pic) return (Pic)o; if (o instanceof JAXBElement && ((JAXBElement)o).getDeclaredType().getName().equals("org.docx4j.dml.picture.Pic") ) { return (Pic)((JAXBElement)o).getValue(); } } return null; }
package org.example.schema; import javax.xml.bind.*; import javax.xml.namespace.QName; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance("org.example.schema"); Root root = new Root(); QName fooQName = new QName("http://www.example.org/schema", "foo"); JAXBElement<String> fooValue = new JAXBElement<String>(fooQName, String.class, "FOO"); root.setFoo(fooValue); ObjectFactory objectFactory = new ObjectFactory(); JAXBElement<String> barValue = objectFactory.createRootBar("BAR"); root.setBar(barValue); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(root, System.out); } }
/** * Reads in a Java object tree from the given XML input. * * @param xml * Reads the entire file as XML. */ public static <T> T unmarshal( File xml, Class<T> type ) { try { JAXBElement<T> item = getContext(type).createUnmarshaller().unmarshal(new StreamSource(xml), type); return item.getValue(); } catch (JAXBException e) { throw new DataBindingException(e); } }
private Authorizations unmarshallAuthorizations() throws JAXBException { try { final XMLStreamReader xsr = XmlUtils.createSafeReader(new StreamSource(authorizationsFile)); final Unmarshaller unmarshaller = JAXB_AUTHORIZATIONS_CONTEXT.createUnmarshaller(); unmarshaller.setSchema(authorizationsSchema); final JAXBElement<Authorizations> element = unmarshaller.unmarshal(xsr, Authorizations.class); return element.getValue(); } catch (XMLStreamException e) { logger.error("Encountered an error reading authorizations file: ", e); throw new JAXBException("Error reading authorizations file", e); } }
public <T> T unMarshall(String xml, Class<T> klass) { try { SAXSource source = new SAXSource(nameSpaceFilter, new InputSource(new StringReader(xml))); return unmarshaller.unmarshal(source, klass).getValue(); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } }
@Test @SuppressWarnings("unchecked") public void unmarshalAnXmlReferingToAWrappedXmlElementDecl() throws Exception { // SPR-10714 unmarshaller = new Jaxb2Marshaller(); unmarshaller.setPackagesToScan(new String[] { "org.springframework.oxm.jaxb" }); unmarshaller.afterPropertiesSet(); Source source = new StreamSource(new StringReader( "<brand-airplane><name>test</name></brand-airplane>")); JAXBElement<Airplane> airplane = (JAXBElement<Airplane>) unmarshaller.unmarshal(source); assertEquals("Unmarshalling via explicit @XmlRegistry tag should return correct type", "test", airplane.getValue().getName()); }
package com.example; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.Unmarshaller; import javax.xml.bind.util.JAXBSource; import javax.xml.namespace.QName; public class Demo { public static void main(String[] args) throws Exception { com.home.Student studentA = new com.home.Student(); studentA.setName("Jane Doe"); studentA.setStatus(com.home.Status.FULL_TIME); JAXBContext contextA = JAXBContext.newInstance(com.home.Student.class); JAXBElement<com.home.Student> jaxbElementA = new JAXBElement(new QName("student"), com.home.Student.class, studentA); JAXBSource sourceA = new JAXBSource(contextA, jaxbElementA); JAXBContext contextB = JAXBContext.newInstance(com.school.Student.class); Unmarshaller unmarshallerB = contextB.createUnmarshaller(); JAXBElement<com.school.Student> jaxbElementB = unmarshallerB.unmarshal(sourceA, com.school.Student.class); com.school.Student studentB = jaxbElementB.getValue(); System.out.println(studentB.getName()); System.out.println(studentB.getStatus().getCode()); } }
@Test @Override @SuppressWarnings("unchecked") public void unmarshalPartialStaxSourceXmlStreamReader() throws Exception { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING)); streamReader.nextTag(); // skip to flights streamReader.nextTag(); // skip to flight Source source = StaxUtils.createStaxSource(streamReader); JAXBElement<FlightType> element = (JAXBElement<FlightType>) unmarshaller.unmarshal(source); FlightType flight = element.getValue(); testFlight(flight); }
@Path("XmlType") @POST public JAXBElement<JaxbXmlType> postXmlType(JaxbXmlType r) { return new JAXBElement<JaxbXmlType>( new QName("jaxbXmlRootElement"), JaxbXmlType.class, r); } }
Object wrapped = ((JAXBElement)value).getValue(); QName qname = ((JAXBElement)value).getName(); JAXBElement contentObject = new JAXBElement( qname, clazz, wrapped); JAXBSource source = new JAXBSource(jc, contentObject); elem = jc.createUnmarshaller().unmarshal(source, clazz); JAXBElement<T> contentObject = new JAXBElement<T>(new QName(clazz.getSimpleName()), clazz, value); JAXBSource source = new JAXBSource(jc, contentObject); elem = jc.createUnmarshaller().unmarshal(source, clazz); } else { @SuppressWarnings("unchecked") T resT = (T) elem.getValue(); res = resT;
context = getContext(((JAXBElement<?>)jaxbObject).getDeclaredType()); } else { Class<?> clazz = jaxbObject.getClass(); if(r==null) { jaxbObject = new JAXBElement(new QName(inferName(clazz)),clazz,jaxbObject); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true); m.marshal(jaxbObject, toResult(xml)); } catch (JAXBException e) { throw new DataBindingException(e); } catch (IOException e) { throw new DataBindingException(e);
QName rootName = new QName(root.getNamespaceURI(), root.getLocalName()); if (!CORE_SCHEMA_ROOT_NAME.equals(rootName)) { throw new XmlConfigurationException("Expecting " + CORE_SCHEMA_ROOT_NAME + " element; found " + rootName); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); ConfigType jaxbModel = unmarshaller.unmarshal(document, configTypeClass).getValue();
/** * Java Collection->Xml with encoding, 特别支持Root Element是Collection的情形. */ public static String toXml(Collection<?> root, String rootName, Class clazz, String encoding) { try { CollectionWrapper wrapper = new CollectionWrapper(); wrapper.collection = root; JAXBElement<CollectionWrapper> wrapperElement = new JAXBElement<CollectionWrapper>(new QName(rootName), CollectionWrapper.class, wrapper); StringWriter writer = new StringWriter(); createMarshaller(clazz, encoding).marshal(wrapperElement, writer); return writer.toString(); } catch (JAXBException e) { throw ExceptionUtil.unchecked(e); } }
public void serializeBody(JAXBElement e, XMLSerializer target, Object outerPeer) throws SAXException, IOException, XMLStreamException { Class scope = e.getScope(); if(e.isGlobalScope()) scope = null; QName n = e.getName(); ElementBeanInfoImpl bi = grammar.getElement(scope,n); if(bi==null) { tbi = grammar.getBeanInfo(e.getDeclaredType(),true); } catch (JAXBException x) { return; Object value = e.getValue(); target.startElement(n.getNamespaceURI(),n.getLocalPart(),n.getPrefix(),null); if(value==null) { target.writeXsiNilTrue();
Unmarshaller unmarshaller = jaxb.createUnmarshaller(); unmarshaller = decorateUnmarshaller(type, annotations, mediaType, unmarshaller); obj = unmarshaller.unmarshal(source); StreamSource source = new StreamSource(new InputStreamReader(entityStream, StandardCharsets.UTF_8)); source.setInputStream(entityStream); obj = unmarshaller.unmarshal(source); obj = unmarshaller.unmarshal(new StreamSource(entityStream)); return element.getValue();
ObjectFactory objFactory = new ObjectFactory(); MyRootElement root = objFactory.createMyRootElement(); ... // Set root properties ... if (!writeDocument(objFactory.createMyRootElement(root), output)) { System.err.println("Failed to marshal XML document"); } ... private boolean writeDocument(JAXBElement document, OutputStream output) { Class<?> clazz = document.getValue().getClass(); try { JAXBContext context = JAXBContext.newInstance(clazz.getPackage().getName()); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); m.marshal(document, output); return true; } catch (JAXBException e) { e.printStackTrace(System.err); return false; } }