简体   繁体   English

在Android中从XML获取数据

[英]Getting data from xml in android

I am working on android application. 我正在开发android应用程序。 In my app I got the xml data response from server and stored it in a string. 在我的应用程序中,我从服务器获取了xml数据响应并将其存储在字符串中。 Now I need to get each value of that xml and display in a dropdown. 现在,我需要获取该xml的每个值并显示在下拉列表中。 How can I do that. 我怎样才能做到这一点。 Please help me with this. 请帮我解决一下这个。 Will be really thankful. 会真的很感激。

My xml data: 我的xml数据:

<?xml version="1.0" encoding="utf-8"?>

<root>
<status>first<status>
<description>very good</description>
<Firstnames>
<name>CoderzHeaven</name>
<name>Android</name>
<name>iphone</name>
</Firstnames>
<SecondNames>
 <name>Google</name>
 <name>Android</name>
</SecondNames>
</root>

I am getting the above mentioned xml data from server. 我从服务器获取上述xml数据。 Now I need to display that in listview. 现在,我需要在列表视图中显示它。 How can I get those values using xmlparser. 如何使用xmlparser获得这些值。 I tried with different examples but it didnt work for me. 我尝试了不同的例子,但对我没有用。

You will need to create an extra class and parametrize your adapter with objects of this class, an example data model would look like: 您将需要创建一个额外的类,并使用该类的对象对适配器进行参数化,示例数据模型如下所示:

  public class DataClass {

private String status, description;
private ArrayList<String> fnames, lnames;

public DataClass() {
    fnames = new ArrayList<String>();
    lnames = new ArrayList<String>();
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

    public ArrayList<String> getFnames() {
    return fnames;
}

public ArrayList<String> getLnames() {
    return lnames;
}
}

As for the XML parser, there are literally tons of examples, you're definitely in advantage if you can use search. 至于XML解析器,实际上有无数的示例,如果可以使用搜索,绝对是您的优势。 Just to give you a staring point, tutorials one , two , three , four . 只是为了给您一个凝视点,教程

If you experience problems, post your efforts and the code that didn't work, what have you tried and so on. 如果遇到问题,请发布您的努力和无效的代码,尝试过的内容等等。 Then you'll get help, otherwise nobody on SO is going to write code for you. 然后,您将获得帮助,否则SO上没有人会为您编写代码。 https://stackoverflow.com/help/how-to-ask https://stackoverflow.com/help/how-to-ask

Here's how you can do it if the xml is inside of your apps assets folder. 如果xml位于您的apps资产文件夹中,则可以按照以下方法进行操作。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    InputStream input = null;
    try {
        input = getApplicationContext().getAssets().open("data.xml");
    } catch (IOException e) {
        e.printStackTrace();
    }
    DocumentBuilder builder = null;
    try {
        builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    Document doc = null;
    if (builder == null) {
        Log.e("TAG", "Builder is empty.");
        return;
    }
    try {
        doc = builder.parse(input);
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (doc == null) {
        Log.e("TAG", "Document is empty.");
        return;
    }

    // Get Firstnames element
    Element firstNames = (Element) doc.getElementsByTagName("Firstnames").item(0);
    // Get name nodes from Firstnames
    NodeList nameNodes = firstNames.getElementsByTagName("name");
    // Get count of names inside of Firstnames
    int cChildren = nameNodes.getLength();
    List<String> names = new ArrayList<String>(cChildren);
    for (int i=0; i<cChildren; i++) {
        names.add(nameNodes.item(i).getTextContent());
        Log.d("TAG","Name: "+names.get(i));
    }

    // Do same with SecondNames
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM