如何用Java将Arraylist的内容写入xml文件

如何用Java将Arraylist的内容写入xml文件,java,xml,arraylist,Java,Xml,Arraylist,这是一个简单的问题。但我无法调试它。我有一个类“Adding.java”,它向ArrayList添加了一些数据 public class Adding { WriteFile ob = new WriteFile(); ArrayList list = new ArrayList(); public void add(){ list.add("Tim"); list.add(2333); list.add(23); list.add("John");

这是一个简单的问题。但我无法调试它。我有一个类“Adding.java”,它向ArrayList添加了一些数据

public class Adding {

WriteFile ob = new WriteFile();

ArrayList list = new ArrayList();
public void add(){

    list.add("Tim");
    list.add(2333);
    list.add(23);

    list.add("John");
    list.add(423);
    list.add(23);

    ob.writeXmlFile(list);

    } }
还有另一个类“WriteFile.java”,它创建一个xml文件

public class WriteFile {

public void writeXmlFile(ArrayList<Object> 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(int i=0; i<list.size(); i ++ ) {

            Element name = doc.createElement("Name");
            name.appendChild(doc.createTextNode(String.valueOf(list.get(i))));
            Details.appendChild(name);

            Element id = doc.createElement("ID");
            id.appendChild(doc.createTextNode(String.valueOf(list.get(i))));
            Details.appendChild(id);


            Element mmi = doc.createElement("Age");
            mmi.appendChild(doc.createTextNode(String.valueOf(list.get(i))));
            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 {
            FileWriter fos = new FileWriter("/home/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");
    }
公共类WriteFile{
公共无效writeXmlFile(ArrayList列表){
试一试{
DocumentBuilderFactory dFact=DocumentBuilderFactory.newInstance();
DocumentBuilder build=dFact.newDocumentBuilder();
Document doc=build.newDocument();
元素根=doc.createElement(“Studentinfo”);
doc.appendChild(根);
元素详细信息=doc.createElement(“详细信息”);
root.appendChild(详细信息);

对于(int i=0;i您需要以3为增量迭代列表:

    for(int i=0; i<list.size() - 2; i += 3 ) {

        Element name = doc.createElement("Name");
        name.appendChild(doc.createTextNode(String.valueOf(list.get(i))));
        Details.appendChild(name);

        Element id = doc.createElement("ID");
        id.appendChild(doc.createTextNode(String.valueOf(list.get(i + 1))));
        Details.appendChild(id);


        Element mmi = doc.createElement("Age");
        mmi.appendChild(doc.createTextNode(String.valueOf(list.get(i + 2))));
        Details.appendChild(mmi);

    }

for(int i=0;i为什么不在列表中存储Student对象,每个对象都有一个名称、一个ID和一个年龄?这将更易于维护,也更易于编程

目前,您在列表中使用相同的索引来查找这三个属性。您需要在循环中按3步进行迭代,并获得元素i、i+1和i+2以使其工作

list.add(new Student("Tim", 2333, 23));
list.add(new Student("John", 423, 23));

...

for (Student student : studentList) {
    ...
    name.appendChild(doc.createTextNode(student.getName()));
    ...
    id.appendChild(doc.createTextNode(Integer.toString(student.getId())));
    ...
    age.appendChild(doc.createTextNode(Integer.toString(student.getAge())));
}

下面是一个清晰XML文件的示例:

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

for(int i=0; i<rectangles.size(); i ++ ) {

    Element rectangle = doc.createElement("Rectangle");
    root.appendChild(rectangle);


    Element width = doc.createElement("width");
 width.appendChild(doc.createTextNode(Double.toString(rectangles.get(i).getWidth())));
    rectangle.appendChild(width);

    Element height = doc.createElement("height");
    height.appendChild(doc.createTextNode(Double.toString(rectangles.get(i).getHeight())));
    rectangle.appendChild(height);

}
Element root=doc.createElement(“RectanglesInfo”);
doc.appendChild(根);

对于(int i=0;我是否确实了解如何向列表中添加条目以及从数组列表中返回的内容?
get(index)
?显然,您不了解,请查看您的书中有关如何使用列表的内容……是的,我已按照您的建议进行了必要的更改。谢谢您的帮助
list.add(new Student("Tim", 2333, 23));
list.add(new Student("John", 423, 23));

...

for (Student student : studentList) {
    ...
    name.appendChild(doc.createTextNode(student.getName()));
    ...
    id.appendChild(doc.createTextNode(Integer.toString(student.getId())));
    ...
    age.appendChild(doc.createTextNode(Integer.toString(student.getAge())));
}
Element root = doc.createElement("RectanglesInfo");
 doc.appendChild(root);

for(int i=0; i<rectangles.size(); i ++ ) {

    Element rectangle = doc.createElement("Rectangle");
    root.appendChild(rectangle);


    Element width = doc.createElement("width");
 width.appendChild(doc.createTextNode(Double.toString(rectangles.get(i).getWidth())));
    rectangle.appendChild(width);

    Element height = doc.createElement("height");
    height.appendChild(doc.createTextNode(Double.toString(rectangles.get(i).getHeight())));
    rectangle.appendChild(height);

}
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<RectanglesInfo>
    <Rectangle>
        <width>89.0</width>
        <height>85.0</height>
    </Rectangle>
    <Rectangle>
        <width>205.0</width>
        <height>212.0</height>
    </Rectangle>
</RectanglesInfo>