使用JDom2在Android上编写XML文件

时间:2013-08-04 11:48:08

标签: java android xml jdom

我正在尝试将JDom版本2.0.5与我的android sdk一起使用。当我尝试运行应用程序时,它总是崩溃,在那里,如果我做一个普通的Java应用程序,它工作正常。我正在尝试将XML文件写入src文件夹。这是logcat日志:

08-04 11:36:53.126: E/dalvikvm(755): Could not find class 'org.jdom2.Document', referenced     from method com.example.touchsensor.MainActivity.writeXML

这是我的代码:

package com.example.touchsensor;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import java.io.*;
import org.jdom2.*;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    writeXML();


}




private static void writeXML() {

    try{
    Document doc = new Document();

    Element theRoot = new Element("tvshows");
    doc.setRootElement(theRoot);

    Element show = new Element("show");
    Element name = new Element("show");
    name.setAttribute("show_id", "show_001");

    name.addContent(new Text("Life On mars"));

    Element network = new Element("network");
    network.setAttribute("country", "US");

    network.addContent(new Text("ABC"));

    show.addContent(name);
    show.addContent(network);

    theRoot.addContent(show);

    // - -



    Element show2 = new Element("show");
    Element name2 = new Element("show");
    name2.setAttribute("show_id", "show_002");

    name2.addContent(new Text("Life On mars"));

    Element network2 = new Element("network");
    network2.setAttribute("country", "US");

    network2.addContent(new Text("ABC"));

    show2.addContent(name2);
    show2.addContent(network2);

    theRoot.addContent(show2);

    XMLOutputter xmlOutput = new XMLOutputter(Format.getPrettyFormat());
    xmlOutput.output(doc, new FileOutputStream(new File("./src/jdomMade.xml")));
    }catch(Exception e){
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:1)

你有没有经历过这个; https://github.com/hunterhacker/jdom/wiki/JDOM2-and-Android。 ?

具体来说,JDOM jar是你android项目的libs文件夹吗?

相关问题