1 | package nl.powercraft.tagtracker;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.FileInputStream;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.util.ArrayList;
|
---|
7 |
|
---|
8 | import javax.xml.parsers.DocumentBuilder;
|
---|
9 | import javax.xml.parsers.DocumentBuilderFactory;
|
---|
10 | import javax.xml.parsers.ParserConfigurationException;
|
---|
11 |
|
---|
12 | import org.w3c.dom.Document;
|
---|
13 | import org.w3c.dom.Element;
|
---|
14 | import org.w3c.dom.Node;
|
---|
15 | import org.w3c.dom.NodeList;
|
---|
16 | import org.xml.sax.SAXException;
|
---|
17 |
|
---|
18 | import android.os.Environment;
|
---|
19 | import android.util.Log;
|
---|
20 |
|
---|
21 | public 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 | }
|
---|