Monday, January 18, 2010

Native payload Parsing

Most of the medium and large organization uses XML format of message to make communication with their customer or vendor. However, still we do fall in native format like EDI, JSON, and etc to transmit data over network. Even in webservice.

XML is the simplest way of transmitting message, however still native format has its own space to popularly used.

Native format defined by Industry standard bodys, and makes organization's life easy by putting hands together to train and develop the community. Specifically, B2B domain runs on top these standards and provisioning to marshall and unmarshalling data from native to xml and xml to native.

Why do we need native format?
Transmitting XML formatted data consumes considerable cost in network than native format.
Large System implemented before XML inventions and found ease of maintenance
Large number of dependencies like customer and vendors system

We may be interested in knowing market share of native and xml payloads. Upto me, I believe still large organizations which transmits very small(less than 1 KB) and very large(100MB) uses native format. XML fits in between of these two extreme.


Scannar


To write converter between native to object/xml or vice versa, we have to write a string parsing program using java.util.StringTokenizer, which will splits the string based on the static delimiters. We do get token only in the form of String, and it is our responsibility of converting into respective primitive format.

java.util.Scanner introduced in Java SE 5.0, in which we can take input either from InputStream, Readable, and String and possible to split using dynamic delimiters. Here, in the below samples, orgScanner used to split Organization name and employee name. Same orgScanner used to split organization name and age.


import java.util.Scanner;

public class ScannerTest {
public static void main(String[] args) {
String input = "Emp Krishna name Dharma Org Oracle " +
  "Age 29 Emp Suresh name Dharma Org Oracle Age 28";
Scanner s = new Scanner(input).useDelimiter("\\s*Emp\\s*");
while (s.hasNext()) {
 processEmployee(s.next());
}
s.close();
}

private static void processEmployee(String empStr) {
Scanner orgScanner = new Scanner(empStr).useDelimiter("\\s*Org\\s*");
if (orgScanner.hasNext()) {
 Scanner nameScanner = new Scanner(orgScanner.next())
   .useDelimiter("\\s*name\\s*");
 System.out.println("FirstName:" + nameScanner.next() + " LastName:"
   + nameScanner.next());
}

if (orgScanner.hasNext()) {
 orgScanner = orgScanner.useDelimiter("\\s*Age\\s*");
 System.out.println("Organization:" + orgScanner.next() + " Age:"
   + orgScanner.nextInt());
} else {
 System.out.println("No Organization specified");
}}
}
 Output:
javac ScannerTest
java ScannerTest

FirstName:Krishna LastName:Dharma
Organization: Org Oracle Age:29
FirstName:Suresh LastName:Dharma
Organization: Org Oracle Age:28

No comments:

Post a Comment

Recent Posts

Unix Commands | List all My Posts

Texts

This blog intended to share the knowledge and contribute to JAVA Community such a way that by providing samples and pointing right documents/webpages. We try to give our knowledege level best and no guarantee can be claimed on truth. Copyright and Terms of Policy refer blogspot.com