Refine search
@Path("XmlType") @POST public JAXBElement<JaxbXmlType> postXmlType(JaxbXmlType r) { return new JAXBElement<JaxbXmlType>( new QName("jaxbXmlRootElement"), JaxbXmlType.class, r); } }
private Map<String, Integer> months = ...; @XmlAnyElement public List<JAXBElement<Integer>> getMonths() { List<JAXBElement<Integer>> elements = new ArrayList<JAXBElement<Integer>>(); for (Map.Entry<String, Integer> month: months.entrySet()) elements.add(new JAXBElement(new QName(month.getKey()), Integer.class, month.getValue())); return elements; }
@XmlElementDecl(name = "brand-airplane") public JAXBElement<Airplane> createAirplane(Airplane airplane) { return new JAXBElement<>(new QName("brand-airplane"), Airplane.class, null, airplane); } }
private static JAXBElement<XhtmlValueType> createElement(final String elementName, String value) { try { final XhtmlValueType element = new XhtmlValueType(); element.value = value; final JAXBElement<XhtmlValueType> jaxbElement = new JAXBElement<XhtmlValueType>( new QName("http://www.w3.org/1999/xhtml", elementName), XhtmlValueType.class, element); return jaxbElement; } catch (Exception e) { throw new RuntimeException(e); } }
private static JAXBElement<XhtmlValueType> createElement(final String elementName, String value) { try { final XhtmlValueType element = new XhtmlValueType(); element.value = value; final JAXBElement<XhtmlValueType> jaxbElement = new JAXBElement<XhtmlValueType>( new QName("http://www.w3.org/1999/xhtml", elementName), XhtmlValueType.class, element); return jaxbElement; } catch (Exception e) { throw new RuntimeException(e); } }
@Path("JAXBElement") @GET public JAXBElement<JaxbXmlType> getJAXBElement() { return new JAXBElement<JaxbXmlType>( new QName("jaxbXmlRootElement"), JaxbXmlType.class, new JaxbXmlType("xml type")); }
package org.example.schema; import javax.xml.bind.JAXBElement; import javax.xml.bind.annotation.*; import javax.xml.namespace.QName; @XmlRegistry public class ObjectFactory { private final static QName _RootFoo_QNAME = new QName("http://www.example.org/schema", "foo"); private final static QName _RootBar_QNAME = new QName("http://www.example.org/schema", "bar"); public Root createRoot() { return new Root(); } @XmlElementDecl(namespace = "http://www.example.org/schema", name = "foo", scope = Root.class) public JAXBElement<String> createRootFoo(String value) { return new JAXBElement<String>(_RootFoo_QNAME, String.class, Root.class, value); } @XmlElementDecl(namespace = "http://www.example.org/schema", name = "bar", scope = Root.class) public JAXBElement<String> createRootBar(String value) { return new JAXBElement<String>(_RootBar_QNAME, String.class, Root.class, value); } }
private static JAXBElement<XhtmlValueType> createElement( final String elementName, String value ) { try { final XhtmlValueType element = new XhtmlValueType(); element.value = value; final JAXBElement<XhtmlValueType> jaxbElement = new JAXBElement<XhtmlValueType>( new QName( "http://www.w3.org/1999/xhtml", elementName ), XhtmlValueType.class, element ); return jaxbElement; } catch ( Exception e ) { throw new RuntimeException( e ); } }
/** * 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); } }
/** * 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); } }
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); } }
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()); } }
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 {
/** * Extract contents of descendant <w:t> elements. * Use this for objects which don't have @XmlRootElement * * @param o * @param w * @param jc * @param uri * @param local * @param declaredType * @throws Exception */ public static void extractText(Object o, Writer w, JAXBContext jc, String uri, String local, Class declaredType) throws Exception { Marshaller marshaller=jc.createMarshaller(); NamespacePrefixMapperUtils.setProperty(marshaller, NamespacePrefixMapperUtils.getPrefixMapper()); marshaller.marshal( new JAXBElement(new QName(uri,local), declaredType, o ), new TextExtractor(w)); }
public XmlNode updateXML(Object jaxbObject, XmlNode xmlNode) throws JAXBException { if(jaxbObject==null || xmlNode==null) throw new IllegalArgumentException(); // TODO // for now just marshal // TODO: object model independenc Element e = (Element)xmlNode; Node ns = e.getNextSibling(); Node p = e.getParentNode(); p.removeChild(e); // if the type object is passed, the following step is necessary to make // the marshalling successful. JaxBeanInfo bi = context.getBeanInfo(jaxbObject, true); if(!bi.isElement()) jaxbObject = new JAXBElement(new QName(e.getNamespaceURI(),e.getLocalName()),bi.jaxbType,jaxbObject); getMarshaller().marshal(jaxbObject,p); Node newNode = p.getLastChild(); p.removeChild(newNode); p.insertBefore(newNode,ns); return (XmlNode)newNode; }
JAXBElement contentObject = new JAXBElement( qname, clazz, wrapped); JAXBElement<T> contentObject = new JAXBElement<T>(new QName(clazz.getSimpleName()), clazz, value); JAXBSource source = new JAXBSource(jc, contentObject); elem = jc.createUnmarshaller().unmarshal(source, clazz);
/** * Receives the root element and determines how to start * unmarshalling. */ @Override public void childElement(UnmarshallingContext.State state, TagName ea) { UnmarshallingContext context = state.getContext(); // unmarshals the specified type QName qn = new QName(ea.uri,ea.local); state.prev.target = new JAXBElement(qn,context.expectedType.jaxbType,null,null); state.receiver = this; // this is bit wasteful, as in theory we should have each expectedType keep // nillable version --- but that increases the combination from two to four, // which adds the resident memory footprint. Since XsiNilLoader is small, // I intentionally allocate a new instance freshly. state.loader = new XsiNilLoader(context.expectedType.getLoader(null,true)); }
new JAXBElement(new QName(uri,local), declaredType, o ), sWriter);
/** Marshal to a W3C document, for object * missing an @XmlRootElement annotation. */ public static org.w3c.dom.Document marshaltoW3CDomDocument(Object o, JAXBContext jc, String uri, String local, Class declaredType) { // TODO - refactor this. try { Marshaller marshaller = jc.createMarshaller(); NamespacePrefixMapperUtils.setProperty(marshaller, NamespacePrefixMapperUtils.getPrefixMapper()); String ignorables = setMcIgnorable(((McIgnorableNamespaceDeclarator)NamespacePrefixMapperUtils.getPrefixMapper()), o); org.w3c.dom.Document doc = XmlUtils.getNewDocumentBuilder().newDocument(); // See http://weblogs.java.net/blog/kohsuke/archive/2006/03/why_does_jaxb_p.html marshaller.marshal( new JAXBElement(new QName(uri,local), declaredType, o ), doc); if (Docx4jProperties.getProperty("docx4j.jaxb.marshal.canonicalize", false)) { byte[] bytes = trimNamespaces(doc, ignorables); DocumentBuilder builder = XmlUtils.getDocumentBuilderFactory().newDocumentBuilder(); return builder.parse(new ByteArrayInputStream(bytes)); } else { return doc; } } catch (Exception e) { throw new RuntimeException(e); } }
if(r==null) { jaxbObject = new JAXBElement(new QName(inferName(clazz)),clazz,jaxbObject);