This project illustrates how to read XML files in Java.
The are 4 main methods for reading XML files:
- DOM - reads the entire xml into an object and allows searching it. see: https://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/
- SAX - stream oriented reading - faster and saves memory. see: https://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/
- XSL based methods, for example XSL Transformation or XPath.
- Text based method, for example using Regular Expressions. This is a less reliable method , but can be faster to implement and/or to run in some cases.
In this example app we parse two XML files: A dataset of all active cellular antennas in Israel, and a dataset of cities in Israel.
We show two alternative ways to parse the first xml: using SAX and Regular Expressions. The second xml is parsed using Regular Expression only.
The data files were downloaded from https://data.gov.il/dataset
-
Make sure that the XML file is encoded in UTF-8
-
When crossing data in run-time, use proper classes. For example we used TreeMap to optimize ranking the results.