XML parsing from site to Android device

I am running an Android application that will parse XML from the Internet. I created several Android apps, but they never did XML parsing, and I was wondering if anyone has any tips on the best way to solve it?

+4
source share
5 answers

Here is an example:

try { URL url = new URL(/*your xml url*/); URLConnection conn = url.openConnection(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(conn.getInputStream()); NodeList nodes = doc.getElementsByTagName(/*tag from xml file*/); for (int i = 0; i < nodes.getLength(); i++) { Element element = (Element) nodes.item(i); NodeList title = element.getElementsByTagName(/*item within the tag*/); Element line = (Element) title.item(0); phoneNumberList.add(line.getTextContent()); } } catch (Exception e) { e.printStackTrace(); } 

In my example, my XML file looks something like this:

 <numbers> <phone> <string name = "phonenumber1">555-555-5555</string> </phone> <phone> <string name = "phonenumber2">555-555-5555</string> </phone> </numbers> 

and I would replace /*tag from xml file*/ with "phone" and /*item within the tag*/ with "string".

+9
source

I always use the w3c dom classes. I have a static helper method that I use to parse xml data as strings and return a Document object to me. Where you can get the XML data may vary (web, file, etc.), but you end up loading it as a string.

something like that...

  Document document = null; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; try { builder = factory.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(data)); document = builder.parse(is); } catch (SAXException e) { } catch (IOException e) { } catch (ParserConfigurationException e) { } 
+1
source

There are various types of parsing mechanisms, one of them is SAX. Here is an example of SAX parsing , the second is DOM analysis. Here is an example of DOM Parsing. . It is not clear from your question what you want, but these can be good starting points.

+1
source

There are three types of parsing that I know: DOM, SAX, and XMLPullParsing.

In my example, here you need the URL and parent element of the XML element node.

 try { URL url = new URL("http://www.something.com/something.xml"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new InputSource(url.openStream())); doc.getDocumentElement().normalize(); NodeList nodeList1 = doc.getElementsByTagName("parent node here"); for (int i = 0; i < nodeList1.getLength(); i++) { Node node = nodeList1.item(i); } } catch(Exception e) { } 

Also try this .

+1
source

I would use the DOM parser, it is not as efficient as SAX if the XML file is not too large, since in this case it is easier.

I created only one Android app that included XML parsing. XML received from the SOAP web service. I used XmlPullParser. The implementation from Xml.newPullParser () had an error where calls to nextText () did not always advance to END_TAG according to the documentation. There is work for this.

0
source

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


All Articles