======serialization demo====================== /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package zolt.newprograms; /** * * @author raju_b */ import java.io.*; public class SerializeDemo { public static void main(String [] args) { Employee e = new Employee(); e.name = "RAJREDDY"; e.address = "HYDERABAD,AP"; e.SSN = 11122333; e.number = 101; e.ram="Hello iam not serialized"; try { FileOutputStream fileOut = new FileOutputStream("emprajreddy"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(e); out.close(); fileOut.close(); }catch(IOException i) { i.printStackTrace(); } } } ======================deserialization demo=========================== /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package zolt.newprograms; /** * * @author raju_b */ import java.io.*; import java.util.Calendar; public class DeserializeDemo { public static void main(String [] args) { Employee e = null; try { FileInputStream fileIn = new FileInputStream("emprajreddy"); ObjectInputStream in = new ObjectInputStream(fileIn); e = (Employee) in.readObject(); in.close(); fileIn.close(); }catch(IOException i) { i.printStackTrace(); return; }catch(ClassNotFoundException c) { System.out.println("Employee class not found."); c.printStackTrace(); return; } System.out.println("Deserialized Employee..."); System.out.println("Name: " + e.name); System.out.println("Address: " + e.address); System.out.println("SSN: " + e.SSN); System.out.println("Number: " + e.number); System.out.println("Number: " + e.ram); System.out.println("Flattened time: " + e.timenew); System.out.println("stored time is: " + e.time); System.out.println(); // print out the current time System.out.println("Current time: " + Calendar.getInstance().getTime()); } } =================================Employee class==================== /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package zolt.newprograms; import java.io.*; import java.util.Calendar; import java.util.Date; /** * * @author raju_b */ public class Employee implements java.io.Serializable { public String name; public String address; public int SSN; transient public String ram; public int number; public Date time; Date timenew=Calendar.getInstance().getTime(); public Employee() { time = Calendar.getInstance().getTime(); System.out.println("the time in emp is="+time); } public Date getTime() { return time; } public void mailCheck() { System.out.println("Mailing a check to " + name + " " + address); } } ========================Marshalling demo=====JavaToXML==================== import java.math.BigDecimal; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; public class JavaToXML { public static void main(String[] args) throws Exception { JAXBContext context = JAXBContext.newInstance(Product.class); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); Product object = new Product(); object.setCode("WI1"); object.setName("Widget Number One"); object.setPrice(BigDecimal.valueOf(300.00)); m.marshal(object, System.out); } } ================Un-Marshalling xmltojava===================== import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; public class XmlToJava { public static void main(String[] args) { try { JAXBContext jc = JAXBContext.newInstance(Product.class); Unmarshaller u = jc.createUnmarshaller(); File f = new File("Product.xml"); Product product = (Product) u.unmarshal(f); System.out.println(product.getCode()); System.out.println(product.getName()); System.out.println(product.getPrice()); } catch (JAXBException e) { e.printStackTrace(); } } }
Tuesday, June 5, 2012
Serialization , Marshalling
Pojo class or been class example
public class NotificationGeoFenceData extends Application{
//int image;
//String sNo, sName, sClass, sPhNo, sEmail;
String Actual, Original, Date, Time,vehicle;
public NotificationGeoFenceData(String vehicle1,String Actual1,String Original1,String Date1,String Time1) {
// TODO Auto-generated constructor stub
Log.v("Activity Name", "NotificationGeoFenceData extends Application");
vehicle=vehicle1;
Actual=Actual1;
Original=Original1;
Date=Date1;
Time=Time1;
Log.v("gggggg","values insterteddddddddddddddddddddddd...........");
}
public String getVehicle() { return vehicle; }
public void setVehicle(String vehicle) { this.vehicle = vehicle; }
public String getActual() { return Actual; }
public void setActual(String Actual) { this.Actual = Actual; }
public String getOriginal() { return Original; }
public void setOriginal(String Original) { this.Original = Original; }
public String getDate() { return Date; }
public void setDate(String Date) { this.Date = Date; }
public String getTime() { return Time; }
public void setTime(String Time) { this.Time = Time; }
}
Total program for custom adapter and parsing
public class NotificationGeoFenceTab extends Activity
{
// String Str = new String("2011-10-24 10:09:52.0");
String Str, Str2;
public static NotificationGeoFenceTab INSTANCE;
ListView GFlist;
// public ArrayList<GeoFenceData> items = new ArrayList<GeoFenceData>();
private static int caseNumber;
private static String xmlData;
private static String loginId;
public ArrayList<NotificationGeoFenceData> geoitem = new ArrayList<NotificationGeoFenceData>();
// int weAreAt = 0;
public NotificationGeoFenceData notificationgeofenceData;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("Activity Name", "NotificationGeoFenceTab extends Activity");
setContentView(R.layout.notification_geofence_alert1);
INSTANCE = this;
GFlist = (ListView) findViewById(R.id.geofencelist);
GFlist.setAdapter(new GeoFenceListAdapter(getApplicationContext(),
R.layout.notification_listdata1_geofencedata,geoitem));
caseNumber = 6;
xmlData = "<zoltit.avts.bean.VehicleInputParam><timeIntervel>"
+ ApplicationVariables.user_minutes
+ "</timeIntervel></zoltit.avts.bean.VehicleInputParam>";
loginId = ApplicationVariables.loginId;
SoapService service = new SoapService();
try {
String result = service.SoapService(caseNumber, xmlData, loginId);
parseAlertsData(result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void parseAlertsData(String xmlRecord) throws Exception {
// TODO Auto-generated method stub
DocumentBuilder db = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlRecord));
Document doc = db.parse(is);
NodeList violation_list = doc.getElementsByTagName("violationlist");
for (int i = 0; i < violation_list.getLength(); i++) {
Element element = (Element) violation_list.item(i);
NodeList geofence_info = doc.getElementsByTagName("geofenceInfo");
for (int j = 0; j < geofence_info.getLength(); j++) {
NodeList geofence_list = element
.getElementsByTagName("geofencelist");
// Toast.makeText(getApplicationContext(),
// "geofenceLength" + geofence_list.getLength(),
// Toast.LENGTH_LONG).show();
for (int k = 0; k < geofence_list.getLength(); k++) {
NodeList vehicle = element
.getElementsByTagName("registratioNo");
NodeList actual_location = element
.getElementsByTagName("actualLocation");
NodeList original_location = element
.getElementsByTagName("originalLocation");
NodeList date = element.getElementsByTagName("date");
Element el_vehicle = (Element) vehicle.item(k);
Element el_actualLocation = (Element) actual_location
.item(k);
Element el_originalLocation = (Element) original_location
.item(k);
Element el_date = (Element) date.item(k);
Str = new String(getCharacterDataFromElement(el_date));
Str2 = new String(
getCharacterDataFromElement(el_originalLocation));
String s[] = Str2.split(",");
String originalLocation = s[0] + "," + s[1];
String date1 = Str.substring(0, 9);
String time1 = Str.substring(10, 16); // Str.length() - 1);
notificationgeofenceData = new NotificationGeoFenceData(getCharacterDataFromElement(el_vehicle),
getCharacterDataFromElement(el_actualLocation),
originalLocation, date1, time1);
geoitem.add(notificationgeofenceData);
}
}
}
}
private String getCharacterDataFromElement(Element e) {
Node child = e.getFirstChild();
if (child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
return cd.getData();
}
return "";
}
private class GeoFenceListAdapter extends ArrayAdapter<NotificationGeoFenceData> {
private ArrayList<NotificationGeoFenceData> GeoFenceDetails;
private Context mContext;
public GeoFenceListAdapter(Context context, int textViewResourceId,
ArrayList<NotificationGeoFenceData> objects) {
super(context, textViewResourceId, objects);
GeoFenceDetails = objects;
mContext = context;
}
@Override
public int getCount() {
return geoitem.size();
}
@Override
public NotificationGeoFenceData getItem(int position) {
return GeoFenceDetails.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vl = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vl.inflate(R.layout.notification_listdata1_geofencedata, null);
}
TextView actualtxt = (TextView) v.findViewById(R.id.txtactual_id);
TextView orinigaltxt = (TextView) v.findViewById(R.id.txtoriginal_id);
TextView datetxt = (TextView) v.findViewById(R.id.txtdate_id);
TextView timetxt = (TextView) v.findViewById(R.id.txttime_id);
TextView vechiletxt= (TextView) v.findViewById(R.id.txtvechile_id);
NotificationGeoFenceData temp = geoitem.get(position);
actualtxt.setText(temp.Actual);
orinigaltxt.setText(temp.Original);
datetxt.setText(temp.Date);
timetxt.setText(temp.Time);
vechiletxt.setText(temp.vehicle);
return v;
}
}
}
XML Parsing
private void parseAlertsData(String xmlRecord) throws Exception {
// TODO Auto-generated method stub
DocumentBuilder db = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlRecord));
Document doc = db.parse(is);
NodeList violation_list = doc.getElementsByTagName("violationlist");
for (int i = 0; i < violation_list.getLength(); i++) {
Element element = (Element) violation_list.item(i);
NodeList geofence_info = doc.getElementsByTagName("geofenceInfo");
for (int j = 0; j < geofence_info.getLength(); j++) {
NodeList geofence_list = element
.getElementsByTagName("geofencelist");
// Toast.makeText(getApplicationContext(),
// "geofenceLength" + geofence_list.getLength(),
// Toast.LENGTH_LONG).show();
for (int k = 0; k < geofence_list.getLength(); k++) {
NodeList vehicle = element
.getElementsByTagName("registratioNo");
NodeList actual_location = element
.getElementsByTagName("actualLocation");
NodeList original_location = element
.getElementsByTagName("originalLocation");
NodeList date = element.getElementsByTagName("date");
Element el_vehicle = (Element) vehicle.item(k);
Element el_actualLocation = (Element) actual_location
.item(k);
Element el_originalLocation = (Element) original_location
.item(k);
Element el_date = (Element) date.item(k);
Str = new String(getCharacterDataFromElement(el_date));
Str2 = new String(
getCharacterDataFromElement(el_originalLocation));
String s[] = Str2.split(",");
String originalLocation = s[0] + "," + s[1];
String date1 = Str.substring(0, 9);
String time1 = Str.substring(10, 16); // Str.length() - 1);
notificationgeofenceData = new NotificationGeoFenceData(getCharacterDataFromElement(el_vehicle),
getCharacterDataFromElement(el_actualLocation),
originalLocation, date1, time1);
geoitem.add(notificationgeofenceData);
}
}
}
}
private String getCharacterDataFromElement(Element e) {
Node child = e.getFirstChild();
if (child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
return cd.getData();
}
return "";
}
Subscribe to:
Posts (Atom)