将文本写入XML需要很长时间--Java

时间:2013-10-23 08:00:50

标签: java xml

我在arraylist中有大约3000个元素。需要将这3000个元素写入xml。 在日食中执行需要花费太多时间,比如20分钟。 有没有有效的方法来做到这一点? 或者对我的代码进行任何修改?

arraylist中的元素应该在将来成长......

MY code snippet ..

---------------------------------------------------
---------------------------------------------------
for(int i=0;i<candidates.size();i++)//Candidates is my arraylist
            {
                String text=candidates.get(i);
                //System.out.println(text);
                text=text+"\n";
                file= new File("./test.xml");
                WriteToXML wr= new WriteToXML(file,"fullname",text);

            }
-------------------------------------------------------
-------------------------------------------------------
//WritetoXML class constructor
public WriteToXML(File xml,String tag,String data)
{
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(false);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new FileInputStream(new File(xml)));
        Element element =  doc.getDocumentElement();
        NodeList node1 = doc.getElementsByTagName(tag);
        Element fn= (Element) node1.item(0);
        Text text = doc.createTextNode(data);
        fn.appendChild(text);
        printtoXML(doc);
    } catch (Exception e) {
            System.out.println(e.getMessage());
            }
}
public static final void printtoXML(Document xml) throws Exception {
    Transformer tf = TransformerFactory.newInstance().newTransformer();
    tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    tf.setOutputProperty(OutputKeys.INDENT, "yes");
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw); 
    DOMSource source = new DOMSource(xml);
    tf.transform(source, result);
    String xmlString = sw.toString();
    File file= new File(xml);
    FileWriter fw=new FileWriter(file,false);
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write(xmlString);
    bw.flush();
    bw.close(); 
}

4 个答案:

答案 0 :(得分:2)

现在,您正在为每个3000个元素执行此操作:

  1. 打开文件
  2. 在那里解析文档
  3. 向dom结构添加元素
  4. 将文件刷新到磁盘并关闭它
  5. 更快的方法是只执行一次步骤1,2和4(循环前1和2;循环后4),然后对列表中的每个元素(循环中)执行步骤3。 只需编写一个新方法,即获取tag变量和Document实例,并将标记添加到文档中。

    这里真正昂贵的是将Object结构多次转换为XML并返回。这有很大的开销。 文件IO也带来了很多开销,但与DOM结构的多次创建和解析相比,这甚至应该很小。

答案 1 :(得分:1)

使用SAX代替DOMSAXDOM

效率更高

答案 2 :(得分:1)

正如@Masud所说,使用SAX没有其他办法。关于这个Generating XML from an Arbitrary Data Structure

有一个很好的例子

答案 3 :(得分:1)

将for循环放在WriteToXML()中。

主要功能:

file= new File("./test.xml");
WriteToXML wr= new WriteToXML(file,"fullname",candidates)

WriteToXML内部

public WriteToXML(File xml,String tag,List candidates )
{
    try {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(false);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new FileInputStream(new File(xml)));
    Element element =  doc.getDocumentElement();
    NodeList node1 = doc.getElementsByTagName(tag);
    Element fn= (Element) node1.item(0);
    for (int i=0;i<candidates.size();i++) { 
        Text text = doc.createTextNode(candidates.get(i)+"\n");
        fn.appendChild(text);
    }
    printtoXML(doc);
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

这样您就不会一直重新解析XML并只编写一次。

我试图做一些微小的改变。我不建议在构造函数中执行此操作 - 除非有充分的理由这样做。