I have read many articles about XML parsing in Android. However, all of them are just code without any explaination. I’m new to Android (and even Java), however, I have been with .NET for a long time. So please explain me some classes:
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
What do they do?
xr.parse(new InputSource(sourceUrl.openStream()));
This parse a XML file from the Internet. How can I parse a XML file in res/raw? And even with a XML file in SD Card?
Is there any class like XMLDocument in .NET? And how can I write XML file into SD Card?
Thank you.
The first three lines just set up an XML parser. The variant of parser is a stream parser, that does not need the full input as a tree in memory (that would be a dom parser), but which reads the xml data as it streams by.
Do read stuff from the assets folder you could use that code:
where
board.loadFromXmlInputStream(is);internally calls your above 3 lines of code and thenxr.parse(new InputSource(is)See e.g. SampleView for the above lines and the Board.
Note that your code above will not be enough to process the XML – you also need some callbacks (like the ones shown in the Board class).
As an alternative you can also use a DOM parser, that creates a tree representation of the XML in memory that you can then walk.