0

I need to make an xml like below using JAXB, But i can't get <front> and <back> under the <result> tag.

    <?xml version="1.0" encoding="UTF-8"?>
<output>
   <option>abc</option>
   <refid>8789</refid>
   <response>
      <responsecode>1234</responsecode>
      <responsedetails>xyz</responsedetails>
   </response>
   <result>
      <front>
         <containimage>Yes</ontainimage>
         <containdetail>No</containdetail>
      </front>
      <back>
         <ontainimage>Yes</ontainimage>
         <containdetail>Yes</containdetail>
      </back>
   </result>
</output>

I am able to get the <response> but not the <result> I am using something like -

@XmlElementWrapper(name = "result")
@XmlElement
public ArrayList<Front> getFront() {
    return front;
}
@XmlElementWrapper(name = "result")
@XmlElement
public ArrayList<Back> getBack() {
    return back;
}

and get the xml as

<?xml version="1.0" encoding="UTF-8"?>
    <output>
       <option>abc</option>
       <refid>8789</refid>
       <response>
          <responsecode>1234</responsecode>
          <responsedetails>xyz</responsedetails>
       </response>
       <result>
          <front>
             <containimage>Yes</ontainimage>
             <containdetail>No</containdetail>
          </front>
        </result>
        <result>
          <back>
             <ontainimage>Yes</ontainimage>
             <containdetail>Yes</containdetail>
          </back>
       </result>
    </output>

I need <front> and <back> inside <result> . Please help

2
  • You could create a result POJO that contains both a front and back POJO Commented Mar 3, 2016 at 15:58
  • Possible duplicate of stackoverflow.com/questions/4889913/… Commented Mar 3, 2016 at 16:06

1 Answer 1

1

I suppose you problem is in the Output class, or by addind a new result instance to our output object... By the way, do you manually create the java binding, or lat JaxB generates it from an the schema?

I propose you the following steps:

  1. create a schema (XSD file) that match your XML
  2. let JaxB generate the java binding
  3. write the File

For the Schema, I created the following one (correcting the spelling of the tag "containimage")

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="output">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="option" type="xsd:string" />
            <xsd:element name="refid" type="xsd:string" />
            <xsd:element name="response">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="responsecode" type="xsd:string" />
                        <xsd:element name="responsedetails" type="xsd:string" />
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
            <xsd:element name="result" maxOccurs="1">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="front" type="resultType" />
                        <xsd:element name="back" type="resultType" />
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

<xsd:complexType name="resultType">
    <xsd:sequence>
        <xsd:element name="containimage" type="xsd:string" />
        <xsd:element name="containdetail" type="xsd:string" />
    </xsd:sequence>
</xsd:complexType>

Then, I asked JaxB generate the Java binding (wizzard in Eclipse), that generates me 3 classes: ObjectFactory, Output and ResultType classes.

Finally, I can write the output file:

package stackoverflow35769423;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import stackoverflow35769423.jaxb.ObjectFactory;
import stackoverflow35769423.jaxb.Output;
import stackoverflow35769423.jaxb.ResultType;

public class CreateResultFile {

    public static void main(String[] args) {    
        try {
            (new CreateResultFile()).writeFile();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }

    private void writeFile() throws JAXBException, FileNotFoundException {  
        OutputStream os = new FileOutputStream("files" + File.separator + "output.xml");
        ObjectFactory factory = new ObjectFactory();
        JAXBContext jaxbContext = JAXBContext.newInstance(factory.getClass().getPackage().getName());
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        Output output = factory.createOutput();
        output.setOption("abc");
        output.setRefid("8789");

        Output.Response reponse = factory.createOutputResponse();
        reponse.setResponsecode("1234");
        reponse.setResponsedetails("xyz");
        output.setResponse(reponse);

        Output.Result result = factory.createOutputResult();
        ResultType resultFront = factory.createResultType();
        resultFront.setContainimage("Yes");
        resultFront.setContaindetail("No");

        ResultType resultBack = factory.createResultType();
        resultBack.setContainimage("Yes");
        resultBack.setContaindetail("Yes");

        result.setFront(resultFront);
        result.setBack(resultBack);
        output.setResult(result);

        jaxbMarshaller.marshal(output, os);
    }
}

Et Voilà :)

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the information, i will definitely try this. Also, is there a way to work this around with xml annotations ?
The above suggestion worked good :)..I need one more advice from you , currently one ObjectFactory is linked to single xml format now can i generate a single ObjectFactory for different xml with different formats ?
The ObjectFactory object is "linked" to one XML schema. I personally use a dedicated java package for each XML Schema. In this way, thinks are consistent and robust. When adaptations are needed, update the schema, let the binding be generated (overridding the previous one). I think you should have a look how to define XSD file for your cases. These tutorial are really good : w3schools.com/xml/schema_intro.asp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.