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 void main(String[] args) throws Exception {
    String xmlString = "<message>HELLO!</message> ";
    JAXBContext jc = JAXBContext.newInstance(String.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    StreamSource xmlSource = new StreamSource(new StringReader(xmlString));
    JAXBElement<String> je = unmarshaller.unmarshal(xmlSource, String.class);
    System.out.println(je.getValue());
}

From source file:UnmarshallingDemo.java

public static void main(String[] args) {
    try {//from ww  w  .j  a v  a2s  .  co  m
        JAXBContext jc = JAXBContext.newInstance("generated");

        Unmarshaller u = jc.createUnmarshaller();

        File f = new File("item.xml");
        JAXBElement element = (JAXBElement) u.unmarshal(f);

        Item item = (Item) element.getValue();
        System.out.println(item.getCode());
        System.out.println(item.getName());
        System.out.println(item.getPrice());
    } catch (JAXBException e) {
        e.printStackTrace();
    }
}

From source file:org.jasig.portlet.data.Importer.java

public static void main(String[] args) throws Exception {
    String dir = args[0];//from w w  w. jav  a 2 s.  co  m
    String importExportContext = args[1];
    String sessionFactoryBeanName = args[2];
    String modelClassName = args[3];
    String serviceBeanName = args[4];
    String serviceBeanMethodName = args[5];

    ApplicationContext context = PortletApplicationContextLocator.getApplicationContext(importExportContext);
    SessionFactory sessionFactory = context.getBean(sessionFactoryBeanName, SessionFactory.class);
    Class<?> modelClass = Class.forName(modelClassName);
    Object service = context.getBean(serviceBeanName);

    JAXBContext jc = JAXBContext.newInstance(modelClass);

    File folder = new File(dir);
    File[] files = folder.listFiles(new ImportFileFilter());

    for (File f : files) {
        StreamSource xml = new StreamSource(f.getAbsoluteFile());
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        JAXBElement je1 = unmarshaller.unmarshal(xml, modelClass);
        Object object = je1.getValue();
        Session session = sessionFactory.getCurrentSession();
        Transaction transaction = session.beginTransaction();

        Method method = service.getClass().getMethod(serviceBeanMethodName, modelClass);
        method.invoke(service, object);
        transaction.commit();
    }
}

From source file:MisspelledPersonUnmarshaller.java

public static void main(String[] args) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance("person");
    Unmarshaller unmarshaller = context.createUnmarshaller();

    JAXBElement<Person> element = unmarshaller.unmarshal(new StreamSource("p.xml"), Person.class);
    Person person = element.getValue();
    System.out.println(person.getFirstName());

}

From source file:webservices.simple_jaxb.UnmarshalRead.java

public static void main(String[] args) {
    try {//from  w  ww .j  av  a2s .  co m
        // create a JAXBContext capable of handling classes generated into
        // the primer.po package
        JAXBContext jc = JAXBContext.newInstance("primer.po");

        // create an Unmarshaller
        Unmarshaller u = jc.createUnmarshaller();

        // unmarshal a po instance document into a tree of Java content
        // objects composed of classes from the primer.po package.
        JAXBElement<?> poElement = (JAXBElement<?>) u.unmarshal(new File("po.xml"));
        PurchaseOrderType po = (PurchaseOrderType) poElement.getValue();

        // examine some of the content in the PurchaseOrder
        System.out.println("Ship the following items to: ");

        // display the shipping address
        USAddress address = po.getShipTo();
        displayAddress(address);

        // display the items
        Items items = po.getItems();
        displayItems(items);

    } catch (JAXBException je) {
        je.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(Foo.class, ObjectFactory.class);

    StringReader xml = new StringReader("<foo><C>Hello World</C></foo>");
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    Foo foo = (Foo) unmarshaller.unmarshal(xml);

    JAXBElement<?> aOrBOrCOrD = foo.aOrBOrCOrD;
    System.out.println(aOrBOrCOrD.getName().getLocalPart());
    System.out.println(aOrBOrCOrD.getDeclaredType());
    System.out.println(aOrBOrCOrD.getValue());
}

From source file:Main.java

public static <T> T unmarshall(String file, Class<T> desiredClass, Class context) throws Exception {
    JAXBContext ctx = JAXBContext.newInstance(context);
    Unmarshaller unMarshaller = ctx.createUnmarshaller();
    JAXBElement<T> object = (JAXBElement<T>) unMarshaller.unmarshal(new File(file));
    return object.getValue();
}

From source file:Main.java

public static <T> T parse(final Class<T> clazz, final InputStream inputStream) {
    try {/*from   w w  w  .j av a  2  s  .  com*/
        final JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
        final Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        final Object deserialized = jaxbUnmarshaller.unmarshal(inputStream);
        if (clazz.isAssignableFrom(deserialized.getClass())) {
            return clazz.cast(deserialized);
        } else {
            final JAXBElement<T> jaxbElement = (JAXBElement<T>) deserialized;
            return jaxbElement.getValue();
        }
    } catch (final JAXBException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static Object parseXmlFileToBeanByJAXB(String path, Class clase) throws Exception {

    JAXBContext context = JAXBContext.newInstance(clase);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    JAXBElement elm = unmarshaller.unmarshal(new StreamSource(new File(path)), clase);

    return elm.getValue();

}

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/*  ww w .  j  ava 2s. 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();
}