1

I want to parse below xml file which has huge data, Here is my xml.

what is the best way to parse from XML to java List? can i use SAXParser for parsing the XML or any other efficient way? I expect , i would have data more than 1 million.

<root>
    <Company>
        <name>TATA</name>
        <Location>
          <id>1</id>
          <name>India</name>
            <branchesList>
                <branches>
                <name>Chennai</name>
                <id>Ch-1</id>
                <status>Active</status>
            </branches>
            <branches>
                <name>Hyderabad</name>
                <id>Hy-1</id>
            <status>In-Active</status>
            </branches>
            <branches>
                <name>Delhi</name>
                <id>De-1</id>
                <status>Active</status>
            </branches>
        </branchesList>
        </Location>
         <Location>
          <id>1</id>
          <name>USA</name>
            <branchesList>
                <branches>
                <name>NewYork</name>
                <id>Nw-1</id>
                <status>Active</status>
            </branches>
            <branches>
                <name>NewJersy</name>
                <id>Ne-1</id>
            <status>In-Active</status>
            </branches>
            <branches>
                <name>Denver</name>
                <id>De-1</id>
                <status>Active</status>
            </branches>
        </branchesList>
        </Location>
      </Company>

    </root>
WhoAmI
  • 57
  • 7
  • 4
    Possible duplicate of [Parsing large XML documents in JAVA](https://stackoverflow.com/questions/15132390/parsing-large-xml-documents-in-java) – geo Jun 06 '17 at 11:08
  • Why java list? You might as well build a dom tree and query with xpath... – vtd-xml-author Jun 07 '17 at 06:58
  • This SO post may help you finding a better/performant solutions https://stackoverflow.com/questions/8791600/can-sax-parsers-use-xpath-in-java – vtd-xml-author Jun 07 '17 at 07:08

1 Answers1

0

I would suggest you look at this answer.

It recommends using SAXParser for big XMLs.

As of having 1 million + Strings in a list in java, in principle there is no issue. You could have as many as Integer.MAX_VALUE-5 iirc.

Documentation for SAXParser is here and is quite complete. You should have no issue taking the elements you want and putting them in a list.

Finally, if you want to have more than a simple list of strings in memory, I would look at other collections like Dictionary.

Demogorii
  • 656
  • 5
  • 16