Android 如何使用XML文件?

Android 如何使用XML文件?,android,xml,parsing,xml-parsing,android-listview,Android,Xml,Parsing,Xml Parsing,Android Listview,我想在ListView中显示大陆的名称,我们称之为listContinents。在选择例如US时,我想显示那里的所有状态,我们称之为ListState。选择一个特定的州后,我想显示该州的所有城市,我们称之为ListCity。选择一个城市后,我想显示有关该城市的一些信息,我们称之为listCityInfo。这是对我上面所说的内容的总结 大陆>>州>>城市>>信息 如何创建一个包含所有信息的XML文件并从中读取?基本上我不知道怎么做。我将信息存储在一个html文件中,因此我应该将其转换为XML文件。

我想在ListView中显示大陆的名称,我们称之为listContinents。在选择例如US时,我想显示那里的所有状态,我们称之为ListState。选择一个特定的州后,我想显示该州的所有城市,我们称之为ListCity。选择一个城市后,我想显示有关该城市的一些信息,我们称之为listCityInfo。这是对我上面所说的内容的总结

大陆>>州>>城市>>信息

如何创建一个包含所有信息的XML文件并从中读取?基本上我不知道怎么做。我将信息存储在一个html文件中,因此我应该将其转换为XML文件。这是路吗

如果XML文件包含所有信息,我将如何读取它。如果XML文件只包含大陆、州或城市,我知道如何读取它们,但当它们都存在时就不知道了。我如何让它工作?我不想为每个州创建超过500个包含城市的XML文件,这将是浪费时间

是否有人可以用任何例子向我说明这是如何工作的,或者将我链接到有用的站点

我会很感激的,谢谢你

  File newxmlfile = new File("/data/com.itwine/emergency.xml");

        try{
                newxmlfile.createNewFile();
        }catch(IOException e){
                Log.e("IOException", "exception in createNewFile() method");
        }
        //we have to bind the new file with a FileOutputStream
        FileOutputStream fileos = null;        
        try{
                fileos = new FileOutputStream(newxmlfile);
        }catch(FileNotFoundException e){
                Log.e("FileNotFoundException", "can't create FileOutputStream");
        }
        //we create a XmlSerializer in order to write xml data
        XmlSerializer serializer = Xml.newSerializer();
        try {
                //we set the FileOutputStream as output for the serializer, using UTF-8 encoding
                        serializer.setOutput(fileos, "UTF-8");
                        //Write <?xml declaration with encoding (if encoding not null) and standalone flag (if standalone not null)
                        serializer.startDocument(null, Boolean.valueOf(true));
                        //set indentation option
                        serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
                        //start a tag called "root"
                        serializer.startTag(null, "root");
                        *//**serializer.startTag(null, "Child1");
                        serializer.endTag(null, "Child1");
                        serializer.startTag(null, "Child2");
                        serializer.attribute(null, "attribute", "value");
                        serializer.endTag(null, "Child2");*//*
                        serializer.startTag(null, "EmailId");
                        serializer.text(txtemailid.getText().toString());
                        serializer.endTag(null,"EmailId");
                        serializer.startTag(null, "PhoneNo");
                        serializer.text(txtphoneno.getText().toString());
                        serializer.endTag(null,"PhoneNo");
                        serializer.endTag(null,"root");
                        serializer.endDocument();
                        //write xml data into the FileOutputStream
                        serializer.flush();
                        //finally we close the file stream
                        fileos.close();
                       Toast.makeText(getApplication(), "xml created",Toast.LENGTH_LONG);
                } catch (Exception e) {
                        Log.e("Exception","error occurred while creating xml file");
                }

是的,有一种方法。您可以使用FileOutputStream编写一个Xmlfile。并使用XMLserializer设置属性和标记。请看我给出的示例。

下面是我将如何做到的

将信息保存到SQlite数据库中,并将其放入资产文件夹。 但如果您熟悉webservice,我建议您使用它。并且使用json,因为xml在Android中是个麻烦 阅读并填充到。 .
那么,创建xml应该相当简单:例如,每个州节点都有多个城市节点;每个城市节点都会有多个信息节点等。只是想知道一个大的xml文件是否是一种可行的方法——进行“查找”可能会很慢,或者将所有内容都保存在内存中会很昂贵。我可能会考虑使用一个数据库来处理内容。HTML文件的大小大约是56.3 KB,所以在解析过程中会很慢吗?现在如何解析xml文件并提取信息?有图坦卡蒙的链接吗?大多数TUT都是通过连接到一个网站来展示的,这不是我正在做的。很难说,因为这取决于最终xml结构的复杂性、使用SAX和DOM的解析器以及运行它的设备。使用SimpleXML或Jackson mapper之类的工具可能只需要您方面最少的解析工作。我想说,任何教程都可以,只要它解释了实际的解析逻辑,并将艰苦的工作委托给后台线程。xml内容从何而来应该是无关紧要的。很抱歉,我投了反对票,因为我只是转储了一堆毫无解释的代码,而没有回答OP问题中最重要的部分:这是我应该走的路吗?我不知道这是不是我应该走的路。我倾向于xml解析器,您不认为它会更方便吗?数据库更方便。但是,如果文件的大小不是很大,如果您对解析xml感到满意,那么仍然可以将其放在资产文件夹中的xml文件中。就性能而言,我不建议使用xml,尤其是在处理大文件时。有不同的方法可以做到这一点。