tags:

views:

72

answers:

4

Hi All, I am trying to parse the client request and based on strucure trying to form a jTree.Sample request is below-

 ^class^="Voucher" MSISDN="566773" TopupValue="500" Tax="56"
                  MyOrder={ ^class^="Order" OrderID="221" OrderAddress="Bangalore/KA" OrderDetails="100 boxes"
                   Company_Info={ ^idx^=1 ^class^="CompanyDetails" CompanyName="MICROSOFT"
                                       CompanyAddress="USA" CompanyEmployees=32 }
                   Travel={ ^idx^=1 ^class^="Transport" TransportType="CAB"
                              Total_Buses=34 MonthlyExpense=455 AIR="NO" RAIL="YES" SEA="NO" }
                    ListOfCities={ ^idx^=2 ^class^="CITYLIST" ^1^="MUMBAI" ^2^="BANGALORE" ^3^="HYDERABAD" }
                    ListOfStates={ ^idx^=3 ^class^="StateList" ^1^="Maharashtra" ^2^="Karnataka" } }
        ListOfCountries={ ^idx^=3 ^class^="CountryList" ^1^="India" ^2^="Bangla Desh" } }

As you can see, root node is Voucher and its attibutes, Order is inside it, CompanyDetails is inside Order and so on. This is just a sample request.Another request may/may not contain say Company_Info. How to parse such kind of requests? I am not asking for code but just the approach.Will i be able to achive this using only String/buffer methods? Request is received as java String.

A: 

Likely thats custom format. You just have to manually parse itu use String.split and will be useful with StringTokenizer class too.

lamida
+1  A: 

I would not try with simple String methods. Things get rather complex over time; even if doing relative straight-forwards recursive-descent parsing (top-to-bottom approach).

Next simplest (on short term) step would be seeing if grammar is simple enough to be handled by regexps.

But if you have used compiler compilers (yacc, bison or java equivalents like Antlr, java_cup) this seems like a good use case. And even if you haven't, might be good to have a look; Antlr seems to be most commonly used, I used java_cup years ago and it worked fine for me.

StaxMan
Thanks.I will try see if I can do it using regex.
@user420760 Have you thought about accepting this answer (or at least upvoting it)?
Aaron Novstrup
+1  A: 

For large string searching and manipulating like this I prefer to use Perl and regular expressions. Java.util.regex has a fairly descent regular expression library for you to perform these types of computations.

http://download-llnw.oracle.com/javase/1.4.2/docs/api/java/util/regex/package-summary.html

BT
+2  A: 

I would hate to have to read/maintain the code that parses that format with String methods or regular expressions.

If you have control over the format, it would be best to switch to a standard like JSON or XML where you can use a library (e.g. the JAXB RI, JaxMeAPI, Jackson) to do most of the work for you. Otherwise, use a tool like ANTLR or parser combinators to define your custom parser using a grammar.

Aaron Novstrup
Agree, and good point on Scala's powerful tools (esp. since it runs on JVM).JSON already has good choices for data binding, Jackson (http://jackson.codehaus.org) for example works similar to JAXB.
StaxMan
@StaxMan Thanks for the Jackson reference. I wasn't aware of it.
Aaron Novstrup