如何将ArrayList保存为XML

时间:2017-03-15 19:03:27

标签: java xml database arraylist

我有对象的arrayList,每个对象都包含以下元素:

  • 泽西数
  • 名字
  • 姓氏
  • 首选位置
  • 目标
  • 协助

    ArrayList <blackTeam> blackTeam = new ArrayList <blackTeam>();
    
    ...
    
    public class blackTeam {
    
    int goals, assists;
    String jerseyNum, firstName, lastName, prefPosition;
    
    blackTeam (String _jerseyNum, String _firstName, String _lastName, String _prefPosition, int _goals, int _assists) {
        jerseyNum = _jerseyNum;
        firstName = _firstName;
        lastName = _lastName;
        prefPosition = _prefPosition;
        goals = _goals;
        assists = _assists;
    }
    }
    

这是我将数据保存为XML的代码:

        try {
                String filepath = "blackTeam.xml";
                 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
                 DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
                 Document doc = docBuilder.parse(filepath);

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

                for (int i = 0; i < blackTeam.size(); i++) {
                    Element player = doc.createElement("Player");
                    root.appendChild(player);

                    Element jerseyNum = doc.createElement("JerseyNum");
                    jerseyNum.appendChild(doc.createTextNode(blackTeam.get(i).jerseyNum));
                    player.appendChild(jerseyNum);

                    Element firstName = doc.createElement("FirstName");
                    firstName.appendChild(doc.createTextNode(blackTeam.get(i).firstName));
                    player.appendChild(firstName);

                    Element lastName = doc.createElement("LastName");
                    lastName.appendChild(doc.createTextNode(blackTeam.get(i).lastName));
                    player.appendChild(lastName);

                    Element prefPosition = doc.createElement("PrefferedPosition");
                    prefPosition.appendChild(doc.createTextNode(blackTeam.get(i).lastName));
                    player.appendChild(prefPosition);

                    Element goals = doc.createElement("Goals");
                    goals.appendChild(doc.createTextNode(String.valueOf(blackTeam.get(i).goals)));
                    player.appendChild(goals);

                    Element assists = doc.createElement("Assists");
                    assists.appendChild(doc.createTextNode(String.valueOf(blackTeam.get(i).assists)));
                    player.appendChild(assists);
                }

                //write the content into xml file
                TransformerFactory transformerFactory = TransformerFactory.newInstance();
                Transformer transformer = transformerFactory.newTransformer();
                DOMSource source = new DOMSource(doc);
                StreamResult result =  new StreamResult(new File(filepath));
                transformer.transform(source, result);

                System.out.println("Done");

            }catch(ParserConfigurationException pce){
                  pce.printStackTrace();
            }catch(TransformerException tfe){
                  tfe.printStackTrace();
            }catch(IOException ioe){
                  ioe.printStackTrace();
            }catch(SAXException sae){
                  sae.printStackTrace();
            }

我希望输出的XML看起来像这个例子:

<blackTeam>
    <player>
        <jerseyNumber>56</jerseyNumber>
        <firstName>John</firstName>
        <lastName>Smith</lastName>
        <preferredPosition>Center</preferredPosition>
        <goals>45</goals>
        <assists>13</assists>
    </player>
    <player>
        <jerseyNumber>43</jerseyNumber>
        <firstName>Bill</firstName>
        <lastName>Jones</lastName>
        <preferredPosition>Defense</preferredPosition>
        <goals>12</goals>
        <assists>27</assists>
    </player>
<blackTeam>

当我运行我的代码时,我得到:

[致命错误] blackTeam.xml:1:1:文件过早结束。 org.xml.sax.SAXParseException; systemId:file:/// D:/Computer%20Science%2012/League_Database/blackTeam.xml; lineNumber:1; columnNumber:1;文件过早结束。

我确信此行导致错误:

Document doc = docBuilder.parse(filepath);

有人可以向我解释为什么我的代码无效并提供解决方案吗?

1 个答案:

答案 0 :(得分:1)

您无法在案例中使用docBuilder.parse()方法。当您已有XML文件并希望获得该XML的Java表示(Document)时,将使用此方法。

由于您要创建新文件,因此您应该使用以下代码:

Document doc = docBuilder.newDocument();