Tuesday, April 8, 2008

Create XML in PHP

// here we are creating a xml document
$doc = new DomDocument('1.0');

//there can be only single root node in an xml, hence we are creating the same
$root = $doc->createElement('product');
$root = $doc->appendChild($root);

//we are setting the attribute of the root node
$root->setAttribute('xmlns:xsi','http://www.w3.org/2001/XMLSchema-instance');

//this is creating a node named as categoryHeirarchy within the root node
$ch = $doc->createElement('categoryHierarchy');
$ch = $root->appendChild($ch);

//this is creating a node named as categoryCode within the categoryHeirarchy node
$cat11 = $doc->createElement('categoryCode');
$cat11 = $ch->appendChild($cat11);
$cat11->setAttribute('code','1');
$cat11->setAttribute('name','division');

this xml will look like.....

<product
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<categoryHierarchy>
<categoryCode code="1" name="division">
</categoryCode>
</categoryHierarchy>
</product>





HashMap Sorting Service



package com.girnarsoft.services;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;


public class HashMapSortingService
{
public void getSort(HashMap m)
{
ArrayList outputList = sortMap(m);
int count = 0;
count = outputList.size();
System.out.println("Sorted List is as follows");
while(count > 0)
{
Map.Entry entry = (Map.Entry) outputList.get(--count);
System.out.println("Key:- "+entry.getKey()+"Value:- "+entry.getValue());
}
}

/**
* This method will use Arrays.sort for sorting Map
* @param map
* @return outputList of Map.Entries
*/
public ArrayList sortMap(Map map)
{
ArrayList outputList = null;
int count = 0;
Set set = null;
Map.Entry[] entries = null;
// Logic:
// get a set from Map
// Build a Map.Entry[] from set
// Sort the list using Arrays.sort
// Add the sorted Map.Entries into arrayList and return

set = (Set) map.entrySet();
Iterator iterator = set.iterator();
entries = new Map.Entry[set.size()];
while(iterator.hasNext()) {
entries[count++] = (Map.Entry) iterator.next();
}

// Sort the entries with your own comparator for the values:
Arrays.sort(entries, new Comparator()
{
public int compareTo(Object lhs, Object rhs) {
Map.Entry le = (Map.Entry)lhs;
Map.Entry re = (Map.Entry)rhs;
return ((Comparable)le.getValue()).compareTo((Comparable)re.getValue());
}

public int compare(Object lhs, Object rhs) {
Map.Entry le = (Map.Entry)lhs;
Map.Entry re = (Map.Entry)rhs;
return ((Comparable)le.getValue()).compareTo((Comparable)re.getValue());
}
});
outputList = new ArrayList();
for(int i = 0; i < entries.length; i++) {
outputList.add(entries[i]);
}
return outputList;
}

}

Monday, April 7, 2008

Request and Response From Main function



// Send data
String result = null;
String urlStr = "http://localhost:8080/Report/testing.do";
String requestParameters =
"name" + URLEncoder.encode("prashant singh", "UTF-8") + "&" +
"age" + URLEncoder.encode("23", "UTF-8") + "&" +
location + URLEncoder.encode("India", "UTF-8");
if (requestParameters != null && requestParameters.length () > 0){
urlStr += "?" + requestParameters;
}

URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", ""+ requestParameters.length());
conn.getInputStream();

// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = rd.readLine()) != null){
sb.append(line);
}
rd.close();
result = sb.toString();
System.out.println(result);

Truncate Number Service -- Formatting in Java

import java.text.DecimalFormat;
import java.text.FieldPosition;

public class TruncateNumberService
{
   public static String format(double number)
   {
     DecimalFormat format = new DecimalFormat("#0.00");
     FieldPosition f = new FieldPosition(0);
     StringBuffer s = new StringBuffer();
     s = format.format(number, s, f);
     return s.toString();
   }
}
 

blogger templates | Make Money Online