2

I have a List where I am storing 2 ArrayLists inside this.

metadataList.addAll(countryList);
metadataList.addAll(languageList);

In the other method, I want to access these two lists. I am trying this code:

List<String> countriesList = metadataValues.get(0);
List<String> languageList = metadataValues.get(1);

Its not allowing me to do above line showing error like: Cannot convert from String to List. Please tell me how can I access these two lists and do further process:

List<String> metadataValues = parseXml(metadata.xml);


System.out.println("metadataValues in first method ---->" + metadataValues.size());
List<String> countriesList = metadataValues.get(0);
List<String> languageList = metadataValues.get(1);
StringBuilder sb = new StringBuilder();
StringBuilder sb1 = new StringBuilder();

if (countriesList.size() == 1 || languageList.size() == 1) {
    for (String cnt: countriesList) {
        video.setCountry(cnt);
    }
    for (String language: languageList) {
        video.setLanguage(language);
    }
}
if (countriesList.size() >= 2 || languageList.size() >= 2) {
    for (String s: countriesList) {
        sb.append(s);
        sb.append(",");
    }
    for (String s1: languageList) {
        sb1.append(s1);
        sb1.append(",");
    }
}
if (sb.length() != 0) {
    sb.deleteCharAt(sb.length() - 1);
    System.out.println("StringBuilder Value ------>" + sb.toString());
    video.setCountry(sb.toString());
}
if (sb1.length() != 0) {
    sb1.deleteCharAt(sb1.length() - 1);
    System.out.println("StringBuilder Value ------>" + sb1.toString());
    video.setLanguage(sb1.toString());
}

Here is parseXml method:

private static List < String > parseXml(String xmlData)
throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    InputSource src = new InputSource();
    src.setCharacterStream(new StringReader(xmlData));
    Document doc = builder.parse(src);
    NodeList nodes = doc.getElementsByTagName("metadata");
    Element line = null;
    Element line1 = null;
    List<String> countryList = new ArrayList<String>();
    List<String> languageList = new ArrayList<String>();
    List<String> metadataList = new ArrayList<String>();
    for (int i = 0; i < nodes.getLength(); i++) {
        Element element = (Element) nodes.item(i);
        NodeList countryNodes = element.getElementsByTagName("Country");
        NodeList languageNodes = element.getElementsByTagName("Language");
        for (int j = 0; j < countryNodes.getLength(); j++) {
            for (int k = 0; k < languageNodes.getLength(); k++) {
                if (countryNodes != null) {
                    line = (Element) countryNodes.item(j);
                    if (line != null) {
                        countryList.add(getCharacterDataFromElement(line));
                    }
                }
                if (languageNodes != null) {
                    line1 = (Element) languageNodes.item(k);
                    if (line1 != null) {
                        languageList
                            .add(getCharacterDataFromElement(line1));
                    }
                }
            }
        }
        metadataList.addAll(countryList);
        metadataList.addAll(languageList);
        // System.out
        // .println("countryList.size() ----->" + countryList.size());
    }
    return metadataList;

}
1
  • perhaps you just want an array of (two) lists... or better, a Map<String, String> to store country and language spoken in this country (I presume) Commented Jul 9, 2015 at 10:46

2 Answers 2

2
list != list of list

Change your declaration of list in parseXml() method.

List<String> metadataList = new ArrayList<String>();

to

List<List<String>> metadataList = new ArrayList<List<String>>();

And add element to it as

metadataList.add(countryList);
metadataList.add(languageList);

Then you are able to retrieve those lists as single elements later.

Sign up to request clarification or add additional context in comments.

1 Comment

@Suresh Atta - Thank you so much Suresh. This is working. I changed my code with the above declarations.
1

Well, you have two List<String> and add all entries (a single entry is a String) to the metadatalist. If you use the list's get() method, you will retreive the String at the specific index.

Either store the two lists in an array to reaccess both or use a List<List<String>> metadatalist. So you use the add() method to add both lists to your metadatalist and can access each list again.

1 Comment

@ kevcodez - Thanks for the explanation. Its true that its giving me as a single entry when I tried the above code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.