How to write an ArrayList to an XML file?

arraylist, java, xml

Solution

public static void main(String[] args) {
    // TODO Auto-generated method stub

    WriteFile ob = new WriteFile();
    ArrayList list = new ArrayList();
    list.add(new details("A", 20, 1));
    list.add(new details("B", 30, 2));

    ob.writeXmlFile(list);
}

// Modify this below class as per your need

class details {
String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

int age;
int id;

public details() {
}

public details(String name_, int age_, int id_) {
    name = name_;
    age = age_;
    id = id_;
}

// below class actually writed

public void writeXmlFile(ArrayList<details> list) {

    try {

        DocumentBuilderFactory dFact = DocumentBuilderFactory.newInstance();
        DocumentBuilder build = dFact.newDocumentBuilder();
        Document doc = build.newDocument();

        Element root = doc.createElement("Studentinfo");
        doc.appendChild(root);

        Element Details = doc.createElement("Details");
        root.appendChild(Details);


        for (details dtl : list) {

            Element name = doc.createElement("Name");
            name.appendChild(doc.createTextNode(String.valueOf(dtl
                    .getName())));
            Details.appendChild(name);

            Element id = doc.createElement("ID");
            id.appendChild(doc.createTextNode(String.valueOf(dtl.getId())));
            Details.appendChild(id);

            Element mmi = doc.createElement("Age");
            mmi.appendChild(doc.createTextNode(String.valueOf(dtl.getAge())));
            Details.appendChild(mmi);

        }

        // Save the document to the disk file
        TransformerFactory tranFactory = TransformerFactory.newInstance();
        Transformer aTransformer = tranFactory.newTransformer();

        // format the XML nicely
        aTransformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");

        aTransformer.setOutputProperty(
                "{http://xml.apache.org/xslt}indent-amount", "4");
        aTransformer.setOutputProperty(OutputKeys.INDENT, "yes");

        DOMSource source = new DOMSource(doc);
        try {
            // location and name of XML file you can change as per need
            FileWriter fos = new FileWriter("./ros.xml");
            StreamResult result = new StreamResult(fos);
            aTransformer.transform(source, result);

        } catch (IOException e) {

            e.printStackTrace();
        }

    } catch (TransformerException ex) {
        System.out.println("Error outputting document");

    } catch (ParserConfigurationException ex) {
        System.out.println("Error building document");
    }
}

Problem

I'm trying to store an `ArrayList` into an XML file so that I can retrieve the information later on and then display it back into the console. Can someone show me the most effective way to do this? EDIT: Heres what I am trying to write into an external file ``` // new user is created Bank bank = new Bank(); System.out.println("Enter your full name below (e.g. John M. Smith): "); String name = scanner.nextLine(); System.out.println("Create a username: "); String userName = scanner.nextLine(); System.out.println("Enter your starting deposit amount: "); int balance = scanner.nextInt(); System.out.print(dash); System.out.print("Generating your information...\n"); System.out.print(dash); int pin = bank.PIN(); String accountNum = bank.accountNum(); User user = new User(name, userName, pin, accountNum, balance); //new user gets added to the array list Bank.users.add(user); System.out.println(user); ``` This all creates a Bank user, which gets thrown into an `ArrayList`, then I want to store their information so that I can come back later and redisplay it.

Original source