source: trunk/TagTracker/src/nl/powercraft/tagtracker/XMLParser.java

Last change on this file was 6, checked in by remons, 13 years ago
File size: 1.9 KB
Line 
1package nl.powercraft.tagtracker;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.IOException;
6import java.util.ArrayList;
7
8import javax.xml.parsers.DocumentBuilder;
9import javax.xml.parsers.DocumentBuilderFactory;
10import javax.xml.parsers.ParserConfigurationException;
11
12import org.w3c.dom.Document;
13import org.w3c.dom.Element;
14import org.w3c.dom.Node;
15import org.w3c.dom.NodeList;
16import org.xml.sax.SAXException;
17
18import android.os.Environment;
19import android.util.Log;
20
21public class XMLParser {
22
23        @SuppressWarnings({ "unchecked", "rawtypes" })
24        public ArrayList init()
25        {
26                ArrayList cardID = new ArrayList();
27                File root = new File(Environment.getExternalStorageDirectory(), "TagTracker");
28            File xmlfile = new File(root, "tags.log");
29                DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
30                DocumentBuilder builder = null;
31                try {
32                    builder = builderFactory.newDocumentBuilder();
33                } catch (ParserConfigurationException e) {
34                    e.printStackTrace(); 
35                }
36                try {
37                    Document document = builder.parse(new FileInputStream(xmlfile));
38                    Element rootElement = document.getDocumentElement();
39                    NodeList nodes = rootElement.getChildNodes();
40                   
41                    for(int i=0; i<nodes.getLength(); i++){
42                      Node node = nodes.item(i);
43                      //Log.d("TagTracker", "[" + i + "] " + node.getNodeName());
44                      if(node instanceof Element){
45                        //a child element to process
46                        Element child = (Element) node;
47                        Log.d("TagTracker", "" + (child.getNodeName() == "tag"));
48                        if(child.getNodeName().equals("tag"))
49                        {
50                                 Log.d("TagTracker", "Toasting...");
51                                 String attribute = child.getAttribute("uid");
52                                 cardID.add(attribute);
53                        }
54                      }
55                    }
56                } catch (SAXException e) {
57                    e.printStackTrace();
58                } catch (IOException e) {
59                    e.printStackTrace();
60                }
61                return cardID; 
62        }
63       
64}
Note: See TracBrowser for help on using the repository browser.