简体   繁体   中英

loop a jaxb marshall

I simply want to loop a jaxb marshal with same xml element but with different ID, unlike my code below is parsing new xml file for each ID and write over the older one .

data[][] is read in from a matrix, data[i][0] representing a list of ID, I want to set these id to customer ID.

      Customer customer = new Customer();
      File file = new File("C:/data.xml");

      for (int i = 0; i < data.length; i++) {
          customer.setId(data[i][0]);

            JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();


            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            jaxbMarshaller.marshal(customer, file);
            jaxbMarshaller.marshal(customer, System.out);


      }

output from code above:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="1"/>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="2"/>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="3"/>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="4"/>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="5"/>

I want all the ID output in one single xml file , Any tips?

You can set the JAXB_FRAGMENT property on the Marshaller to prevent the header from being written out. This is necessary when marshalling into another document.

package forum12925616;

import java.io.*;
import javax.xml.bind.*;

public class Demo {

    private static int X = 5;
    private static int Y = 2;

    public static void main(String[] args) throws Exception {
        int[][] data = new int[X][Y];
        for (int x = 0; x < X; x++) {
            for (int y = 0; y < Y; y++) {
                data[x][y] = x;
            }
        }

        // Create this once
        JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

        marshal(jaxbMarshaller, data, System.out);

        FileOutputStream fileOutputStream = new FileOutputStream("src/forum12925616/out.xml");
        marshal(jaxbMarshaller, data, fileOutputStream);
        fileOutputStream.close();
    }

    private static void marshal(Marshaller jaxbMarshaller, int[][] data, OutputStream outputStream) throws Exception {
        OutputStreamWriter writer = new OutputStreamWriter(outputStream);
        writer.write("<?xml version='1.0' encoding='UTF-8' standalone='yes'?>");
        writer.write("\n<customers>\n");
        for (int i = 0; i < data.length; i++) {
            Customer customer = new Customer();
            customer.setId(data[i][0]);
            jaxbMarshaller.marshal(customer, writer);
            writer.write("\n");
        }
        writer.write("<customers>");
        writer.flush();
    }

}

System and File Output

Below is the resulting output to both the console and the file.

<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<customers>
<customer id="0"/>
<customer id="1"/>
<customer id="2"/>
<customer id="3"/>
<customer id="4"/>
<customers>

For More Information

Just move the construction of the marhsaller out of the loop. You can reuse the marshaller as many times as you want in the same thread.

Or are you really trying to output a list of customers as a single xml file? Then you need an array of Customer objects, and then marshall the array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM