简体   繁体   中英

java string parsing

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.

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 (eg 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.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM