0

I'm having some issue while converting String object to JAXBElement string object where I need to set this one

This is the target method where I need to set the value

public void setData(JAXBElement<String> value) {
    this.data = ((JAXBElement<String> ) value);
}

For this one, I have written code something like this

 ObjectFactory factory = new ObjectFactory();
    JAXBElement<ApplicationIngestionRequest> jaxbElement =  new JAXBElement(
            new  QName(ApplicationIngestionRequest.class.getSimpleName()), ApplicationIngestionRequest.class, request);

    StringWriter writer = new StringWriter();
    JAXBContext context =  JAXBContext.newInstance(ApplicationIngestionRequest.class);
    context.createMarshaller().marshal(jaxbElement, writer);
    LOG.info("JAXBElement object :\n"+ writer.toString());
    Unmarshaller u = context.createUnmarshaller();
    JAXBElement<ApplicationIngestionRequest> o = (JAXBElement<ApplicationIngestionRequest>) u.unmarshal(new StringReader(writer));

Log gives me following output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ApplicationIngestionRequest><BranchCode></BranchCode><SourceCode>0000005511</SourceCode></ApplicationIngestionRequest>

Now when I try to set into the method as

losRequest.setData(o.toString());

It doesn't allow me to set as its expecting as JAXBElement format. Any ideas would be greatly appreciated.

Syed
  • 2,471
  • 10
  • 49
  • 89
  • 1) What is `o`? 2) Why do you cast `value` to the same type with which it is declared (it's unnecessary)? 3) Calling `toString()` on any object returns a ... well, it returns a string, not a `JAXBElement`. What exactly are you trying to achieve? 4) "It doesn't allow me." What error do you get? – Seelenvirtuose Apr 21 '15 at 06:21
  • @Seelenvirtuose I have updated the question.. I just want to convert that XML output as JAXBElement string so that I can set it in that method. – Syed Apr 21 '15 at 06:28

1 Answers1

0

As per the code snippet [setData(JAXBElement value)] , setData , accept instance of '(JAXBElement'). But here , you are trying to set a string value [losRequest.setData(o.toString())] . Here you have to set an instance of 'JAXBElement' .This could be the issue.

Ajil Mohan
  • 7,299
  • 2
  • 18
  • 18