Parse/Read XML elements from List of String in java

550 views Asked by At

I have an XML file as List<String> means each line of the XML as String in List. Now I am looking for different(& best) ways to parse/read this XML data from List.

Previously I have worked on parsing XML files directly with the help of DocumentBuilder eg document builder.parse(new File(abc.xml). It makes it very easy to get elements by tag names.

Sample XML in List<String> :

<MainTag>
  <InnerTag>
    <userid>abc</userid>            
    <phone>
            <number>123456</number>
    </phone>
    <phone>
              <number>345678</number>
    </phone>
  </InnerTag>
  <InnerTag>
    <userid>xyz</userid>            
    <phone>
             <number>0987876</number>
    </phone>
   </InnerTag>
 </MainTag>

Sample Output:

abc
   123456
   345678
xyz
   0987876

Question : What is best, reading elements by looping through the List<String> or use any Parser (XML parser) to convert List<String> to Document and NodeList? If using Parser is the best option then which one? (I have used Javax.xml.parser).

Notes: Data will be in large size. XML file size could be approx 100MB.. (Speed and less utilization of memory are the keys.)

(There is also an option to create POJOs as the format of XML structure is not going to change much and hold these values as a list of POJOs but I am not preferring it as these values are just one time read purpose.)

0

There are 0 answers