package gr.mybook.myxml;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.jar.Attributes;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;

public class MainActivity extends ListActivity {
		// XML node keys
		static final String KEY_ITEM = "item"; // parent node
		static final String KEY_NAME = "name";
		static final String KEY_WEBSITE = "website";
		static final String KEY_CATEGORY = "category";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
		
		/** Create a new textview array to display the results */
		try {
		  URL url = new URL("http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml");
		  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		  DocumentBuilder db = dbf.newDocumentBuilder();
		  // If xml document is a file in assets folder
//		  InputStream in_s = getApplicationContext().getAssets().open("example.xml");
//		  Document doc = db.parse(new InputSource(in_s));
		  Document doc = db.parse(new InputSource(url.openStream()));
		  doc.getDocumentElement().normalize();
		  NodeList nl = doc.getElementsByTagName(KEY_ITEM);
		  
			// looping through all item nodes <item>
			for (int i = 0; i < nl.getLength(); i++) {
				// creating new HashMap
				HashMap<String, String> map = new HashMap<String, String>();
				Element e = (Element) nl.item(i);
				// adding each child node to HashMap key => value
				map.put(KEY_NAME, getValue(e, KEY_NAME));
				map.put(KEY_WEBSITE, getValue(e, KEY_WEBSITE));
				map.put(KEY_CATEGORY, getValue2(e, KEY_WEBSITE));

				// adding HashList to ArrayList
				menuItems.add(map);
			}
		} catch(Exception e){
			e.printStackTrace();
		}
		// Adding menuItems to ListView
				ListAdapter adapter = new SimpleAdapter(this, menuItems,
						R.layout.list_item,
						new String[] { KEY_NAME, KEY_WEBSITE, KEY_CATEGORY }, new int[] {
								R.id.name, R.id.website, R.id.category });

				setListAdapter(adapter);

	}

		 public String getValue(Element item, String str) {		
				NodeList n = item.getElementsByTagName(str);
				Node elem = n.item(0);
				Node child;
			     if( elem != null){
			         if (elem.hasChildNodes()){
			             for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
			                 if(child.getNodeType() == Node.TEXT_NODE){
			                       return child.getNodeValue();
			                 }
			             }
			         }
			     }
			     return "";
			} 
	
	
		 public String getValue2(Element item, String str) {		
			 NodeList n = item.getElementsByTagName(str);
			 Node elem = n.item(0);
			 Element fin = (Element) elem;
			String arg1 = fin.getAttribute("category");
		    return arg1;
	
		 } 
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
