May 062011
For my diploma-thesis I need to unmarshall a xml-string. In principle this is no problem, but the xml-string represents a JAXB-Entity that is not annotated with @XmlRootElement. This causes an exception during unmarshalling. Nevertheless you can unmarshall xml using JAXBElement-class.
private YourClass unmarshallFromString(String s) throws JAXBException { { JAXBElement<YourClass> ret = null; JAXBContext jaxbCtx = JAXBContext.newInstance(YourClass.class.getPackage().getName()); Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller(); //Throw exception if problems occur during parsing unmarshaller.setEventHandler(new ValidationEventHandler() { public boolean handleEvent(ValidationEvent event) { throw new RuntimeException(event.getMessage(), event.getLinkedException()); } }); Source src = new StreamSource(new StringReader(s)); ret = unmarshaller.unmarshal(src, EventSources.class); return (YourClass) ret.getValue(); }
Sorry, the comment form is closed at this time.