0

I am currently parsing an XML document and storing the XML data in a String[]. This is what I do:

db = dbf.newDocumentBuilder();
Document dom = db.parse(in);
Element docElem = dom.getDocumentElement();
NodeList valuesList= docElem.getElementsByTagName("values");

Element value = (Element) valuesList.item(0);

//Split on every white space
String[] myValues = value.getFirstChild().getNodeValue().split("\\s+");

System.out.println(myValues.length);
//Output: 5400 

for(String val : myValues){
    System.out.println("Value: " + val);
    //This prints out random amount of lines in the console, see figure.
}

The output from the loop varies. See the picture below.

sysout

The result could be 4145 the first execution and 4198 the second time.

Shouldn't this output be 5400 lines since the String[] holds 5400 items? Why is it not?

Thankful for any kind of explanation.

Marcus

Marcus
  • 6,697
  • 11
  • 46
  • 89

2 Answers2

3

System.out goes to logcat and the logcat is just a buffer with limited capacity. When the capacity is exceeded, older entries are thrown out. The proportion of your System.out messages in the buffer depends on other logcat activity in the system.

See What is the size limit for Logcat and how to change its capacity?

Community
  • 1
  • 1
laalto
  • 150,114
  • 66
  • 286
  • 303
0

If Logcat size limit is not your problem then try the following as well -

    //Split on every white space
    String[] myValues = value.getFirstChild().getNodeValue().split("\\s+");

    System.out.println(myValues.length);
    //Output: 5400 

    List<String> stringList = Arrays.asList(myValues);

    List<String> list = Collections.synchronizedList(stringList);

    synchronized(list) 
    {
       Iterator<String> iterator = list.iterator(); 

       while (iterator.hasNext())
          System.out.println(iterator.next());
    }
sjain
  • 23,126
  • 28
  • 107
  • 185