How to read arbitrary XML into a generic object in Android?

I have a whole bunch of XML sources that I need to read. They have no patterns and, although they are all well formed and contain nothing but strings, they also do not share the structure; some of them are only on one level, others are several, some contain duplicate blocks / records, while others contain a bunch of different blocks describing various aspects of a particular record.

I used to use javax.xml.parsers.SAXParser using a special handler for entering XML data and used it to set the fields of a user object, which was fine when I read only one thing with a fixed structure and the number of records that I knew beforehand , and no sub-keys or sub-keys. In PHP, I would read all of this in a multidimensional associated array, but Java does not seem to have the exact equivalent.

I suppose I need some kind of Map or List , but I don't understand what type or how to use it. Is there a standard Android-y way to do this?

+4
source share
2 answers

You can have a List of Maps for modeling a multidimensional array in PHP.

 List<Map<String, String>> multidimArray = new ArrayList<Map<String, String>>(); // This is the first element in Array Map<String, String> map = new HashMap<String, String>(); map.put("key1", "value1"); map.put("key2", "value2"); map.put("key3", "value3"); multidimArray.add(map); 
+1
source

There are several different standard XML object models: you must use one of them. This article Working with XML on Android gives a brief description of some of the options for Android. The DOM is the document object model that is the most widely available. Its API is terrible for working directly, but you can request it using XPath , which is a convenient syntax for accessing various nodes in the XML tree (see javax.xml.xpath | Android Developer ).

+1
source

Source: https://habr.com/ru/post/1479244/


All Articles