如何使用XML为我的测验制作者存储数据?

时间:2013-10-08 02:25:31

标签: java android xml store

当前代码:https://github.com/coldblade2000/QuizMaker/tree/Development 我做的XML:http://pastebin.com/FBHVsnPA

我正在为Android制作一个Quiz Maker应用程序,并且无法使用XML来存储数据。我做了一个小的XML文件,我用它来测试它。它有1个问题的空间和4个可能的答案。

我无法理解如何向XML添加值和字符串,阅读它等等。我已经尝试了大多数XML和Android教程,但大多数都没有讨论在其中使用变量或添加自己的属性对元素。 XML对我来说真的很新,我只知道基本的HTML会让我

1 个答案:

答案 0 :(得分:1)

可用于读取和写入XML的基本Java API for XML DOM StaX。然而,DOM4J可以更好,因为它具有更简单和简单的API。

这是一种创建XML文档的简单方法

public void createXML() throws IOException {

    Document document = DocumentHelper.createDocument();
    Element rootElement = document.addElement("Students");
    Element studentElement = rootElement.addElement("student").addAttribute("country", "USA");
    studentElement.addElement("id").addText("1");
    studentElement.addElement("name").addText("Peter");
    XMLWriter writer = new XMLWriter(new FileWriter("Students.xml"));
    //Note that You can format this XML document
    /*
     * FileWriter output = new FileWriter(new File("Students.xml"));
    OutputFormat format = OutputFormat.createPrettyPrint();
    XMLWriter writer = new XMLWriter(output,format);<- will fomat the output
     */

    //You can print this to the console and see what it looks like
    String xmlElement = document.asXML();
    System.out.println(xmlElement);
    writer.write(document);
    writer.close();

}

阅读student.xml

public void readXML() throws SAXException, IOException,
        ParserConfigurationException, DocumentException {
    /*Integration with DOM 
    DOMReader reader = new DOMReader();
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = reader.read(builder.parse(new File("Students.xml")));
    */
    SAXReader readerSAX = new SAXReader();
    Document document2 = readerSAX.read(new File("Students.xml"));
    Element root = document2.getRootElement();
    Student student = null;
    List<Student> studentsList = new ArrayList<Student>();
    if (root.getName().equalsIgnoreCase("students")) {
        for (@SuppressWarnings("unchecked")
        Iterator<Student> i = root.elementIterator(); i.hasNext();) {
            Element element = (Element) i.next();
            if ("student".equalsIgnoreCase(element.getName())) {
                student = new Student();
                for (int j = 0, size = element.nodeCount(); j < size; j++) {
                    Node node = (Node) element.node(j);
                    if (node instanceof Element) {
                        if ("id".equalsIgnoreCase(node.getName())) {
                            student.setId(Integer.parseInt(node.getText()));
                        } else if ("name".equalsIgnoreCase(node.getName())) {
                            student.setName(node.getText());
                        }
                    }
                }
                studentsList.add(student);
            }
        }
    }
    for(Student stud : studentsList){
        System.out.println(stud);           
    }
}