Search This Blog

Wednesday, June 20, 2012

DOM parsing using xml

Put the following code in the xml file.

<Books>
    <Book>
        <Name>Cryptography</Name>
        <Author>Harish</Author>
        <Price>$200</Price>
    </Book>
    <Book>
        <Name>Android</Name>
        <Author>Harish</Author>
        <Price>$250</Price>
    </Book>
</Books>


Method to parse this as the xml file and read in the method.

public HashMap doDomParsing(String strUrl, String strParent, String[] child)
{
       HashMap hmap, hmapchild = null;
       URL url;
       hmap = new HashMap();
       
       try
       {
           //url = new URL(strUrl);
           
           DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
           DocumentBuilder db = dbf.newDocumentBuilder();

          Document doc = db.parse(new InputSource(getClass().getResourceAsStream("/res/raw/parse.xml")));
           
           NodeList nodeList = doc.getElementsByTagName("item");
           for (int i = 0; i < nodeList.getLength(); i++)
           {
               
               Node node = nodeList.item(i);
               NodeList properties = node.getChildNodes();
               
               hmapchild = new HashMap();
               
               for(int j=0 ; j < properties.getLength() ; j++)
               {
                   Node property = properties.item(j);
                   String nodename = property.getNodeName();
                   Log.e("Node NAME", nodename);
                   
                   for(int inew =0 ;inew < child.length ; inew++)
                   {
                       if(nodename.equalsIgnoreCase(child[inew]))
                       {
                           hmapchild.put(nodename, property.getFirstChild().getNodeValue());
                       }
                   }
               }
               hmap.put(i, hmapchild);
           }
       }
       catch (Exception e)
       {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
       
       return  hmap;
}

Thursday, June 7, 2012

How to get the battery level And the phone number of the phone

For the bettery level of the phone we need to write the following code.


private void batteryLevel() {
             BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
                       public void onReceive(Context context, Intent intent) {
                                    context.unregisterReceiver(this);
                                    int rawlevel = intent.getIntExtra("level", -1);
                                    int scale = intent.getIntExtra("scale", -1);
                                    int level = -1;
                                    if (rawlevel >= 0 && scale > 0) {
                                                       level = (rawlevel * 100) / scale;
                                    }
             batterLevel.setText("Battery Level Remaining: " + level + "%");
         }
        };
   IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
   registerReceiver(batteryLevelReceiver, batteryLevelFilter);
}



Here level indicates the bettery level of the phone.


For getting the phone number of the phone we have to insert the following code...


TelephonyManager phoneManager = (TelephonyManager) getApplicationContext()                            
                                                                     .getSystemService(Context.TELEPHONY_SERVICE);
String phoneNumber = phoneManager.getLine1Number();

Here phoneNumber indicates the number of the current phone.