Example usage for javax.xml.bind JAXBElement getValue

List of usage examples for javax.xml.bind JAXBElement getValue

Introduction

In this page you can find the example usage for javax.xml.bind JAXBElement getValue.

Prototype

public T getValue() 

Source Link

Document

Return the content model and attribute values for this element.

See #isNil() for a description of a property constraint when this value is null

Usage

From source file:Main.java

public static Object parseXmlStringToBeanByJAXB(String xml, Class clase) throws Exception {

    JAXBContext context = JAXBContext.newInstance(clase);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    InputStream is = null;//from   ww  w .  jav  a 2 s. com
    try {
        is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    if (is == null) {
        return null;
    }

    JAXBElement elm = unmarshaller.unmarshal(new StreamSource(is), clase);

    return elm.getValue();

}

From source file:Main.java

public static Object xmlToJaxb(Class<?> xmlClass, InputStream is) throws JAXBException, IOException {
    JAXBContext jaxbContext = JAXBContext.newInstance(xmlClass);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    JAXBElement<?> element = (JAXBElement<?>) unmarshaller.unmarshal(getReader(is));
    return element.getValue();
}

From source file:Main.java

public static <BoundType> BoundType unmarshallJAXBElement(JAXBElement<? extends BoundType> v) {
    if (v == null) {
        return null;
    } else {//from  w  w w .j  a  v  a 2  s .  c  o  m
        return v.getValue();
    }
}

From source file:Main.java

public static String getXmlString(JAXBElement versioningInfo, Boolean formatXml, Schema schema)
        throws JAXBException {
    String packageName = versioningInfo.getValue().getClass().getPackage().getName();
    JAXBContext context = JAXBContext.newInstance(packageName);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formatXml);

    if (schema != null) {
        marshaller.setSchema(schema);//from   w  ww. j av a  2 s. c  om
    }

    ByteArrayOutputStream oStream = new ByteArrayOutputStream();
    marshaller.marshal(versioningInfo, oStream);

    return oStream.toString();
}

From source file:Main.java

/**
 * Converting DOM Element object into JAXB OASIS XML Object
 * @param <T>/*  w w w. j a va 2s .  c o  m*/
 * @param cls
 * @param domElement
 * @return
 */
public static <T> T marshal(Class<T> cls, Element domElement) {
    try {
        JAXBContext jc = JAXBContext.newInstance(cls);
        javax.xml.bind.Unmarshaller unmarshaller = jc.createUnmarshaller();
        JAXBElement<T> jaxbObject = unmarshaller.unmarshal(domElement, cls);

        T object = jaxbObject.getValue();

        return object;
    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Attempts to unmarshal an XML document from an InputStream.
 *
 * @param inputStream InputStream to unmarshal XML from.
 * @param type        JAXB class of the returned object. Must be in the same package as other relevant JAXB classes.
 * @param <T>         the type of the returned object
 * @return the unmarshalled JAXB object//from  w ww. j  a  v  a2 s  .co m
 * @throws JAXBException if an unexpected error occurs while unmarshalling
 */
public static <T> T unmarshal(InputStream inputStream, Class<T> type) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(type.getPackage().getName());
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    @SuppressWarnings("unchecked")
    JAXBElement<T> jaxbElement = (JAXBElement<T>) unmarshaller.unmarshal(inputStream);
    return jaxbElement.getValue();
}

From source file:Main.java

public static String readComplexProperty(String name, List<Object> objects, String methodName) {
    for (Object o : objects) {
        if (o instanceof JAXBElement) {
            JAXBElement element = (JAXBElement) o;
            if (name.equals(element.getName().getLocalPart())) {
                return callMethod(element.getValue(), methodName);
            }/*  w w w .  j  a v a 2s  .  c  om*/
        }
    }
    return null;
}

From source file:eu.scape_project.tool.toolwrapper.data.tool_spec.utils.Utils.java

/**
 * Unmarshals an input stream of xml data to a Tool.
 *///from w  ww . j av  a 2 s .co m
public static Tool fromInputStream(InputStream input) throws JAXBException, IOException, SAXException {
    Unmarshaller unmarshaller = Utils.createUnmarshaller();
    JAXBElement<Tool> unmarshalled = unmarshaller.unmarshal(new StreamSource(input), Tool.class);
    return unmarshalled.getValue();
}

From source file:com.dnastack.bob.rest.BasicTest.java

public static Object readObject(Class c, String url) throws JAXBException, MalformedURLException {
    JAXBContext jc = JAXBContext.newInstance(c);

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    unmarshaller.setProperty(JAXBContextProperties.MEDIA_TYPE, "application/json");
    unmarshaller.setProperty(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
    StreamSource source = new StreamSource(url);
    JAXBElement jaxbElement = unmarshaller.unmarshal(source, c);

    return jaxbElement.getValue();
}

From source file:de.cosmocode.palava.salesforce.sync.NullFieldCollector.java

private static void collect(Set<String> nullFields, Object object) {
    LOG.trace("Collecting null fields on {}", object);
    final Field[] fields = object.getClass().getDeclaredFields();
    for (Field field : fields) {
        if (Modifier.isStatic(field.getModifiers()))
            continue;
        if ("fieldsToNull".equals(field.getName()))
            continue;
        try {/* w w  w.  j  av a  2s  . co m*/
            final boolean accessible = field.isAccessible();
            field.setAccessible(true);
            final Object value = field.get(object);
            field.setAccessible(accessible);
            if (value == null) {
                continue;
            } else if (value instanceof JAXBElement<?>) {
                final JAXBElement<?> jaxb = JAXBElement.class.cast(value);
                if (jaxb.getValue() == null) {
                    nullFields.add(nameOf(field));
                }
            }
        } catch (IllegalAccessException e) {
            throw new IllegalArgumentException(e);
        }
    }
    LOG.trace("Null fields on {}: {}", object, nullFields);
}