Import Scala. collection. mutable. listbufferimport Scala. collection. immutable. {map => SMAP}; // immutable import Scala. collection. mutable. {map => xmap}; // variable object main {def main (ARGs: array [String]): unit = {Val S = "" <? XML version = "1.0" encoding = "UTF-8" standalone = "yes"?> <Conf> <dbcps> <Test> <minidle> 1 </minidle> <maxidle> 2 </maxidle> | <maxactive> 2 </maxactive> <URL> JDBC: mySQL: // localhost/test </URL> <driverclassname> COM. mySQL. JDBC. driver </driverclassname> | <initialsize> 2 </initialsize> <password> </password> <username> root </username> <maxwait> 500 </maxwait> </test> <class> Org. apache. tomcat. DBCP. DBCP. basicdatasource </class> </dbcps> | <about> <author> Fred </author> <ver> 1.0.0 </VER> </abo Ut> <DBCS> <Test> <URL> JDBC: mysql: // localhost/test </URL> <driverclassname> COM. mySQL. JDBC. driver </driverclassname> <password> | </password> <username> root </username> </test> </DBCS> <log> <level> all </level> <path>/home/wengmj/</path> </log> </conf> """. stripmargin; Val M = xml. parse (s); println (m) ;}} object XML {def parse (S: string): SMAP [string, any] = {If (S = NULL | S. isempty) return NULL; Val Lex = New XML (s ); Val map = xmap [String, any] (); var n = Lex. next; If (n = NULL) return NULL; N. _ 1 match {Case "0" => map (N. _ 2) = parsevalue (N. _ 3); Case "1" => map (N. _ 2) = parsenode (lex) Case "2" => map (N. _ 2) = n. _ 2; Case _ =>} return SMAP. empty ++ map;} private def parsevalue (V: string): Any = {v match {Case "true" => true; Case "false" => false; case "null" => NULL; case _ => numbertype (v) Match {Case 1 => retu Rn v. toint; Case 2 => return v. tolong; Case 3 => return v. tofloat; Case _ => return v ;}} private def parsenode (LEX: XML): Any = {var S = Lex. next; If ("row" = S. _ 2) {Val list = listbuffer [any] (); do {S. _ 1 match {Case "0" => list. append (parsevalue (S. _ 3); Case "1" => list. append (parsenode (lex); Case "2" => list. append (S. _ 3); Case "-1" => return list. tolist;} s = Lex. next;} while (s! = NULL); return list. tolist;} Val map = xmap [String, any] (); do {S. _ 1 match {Case "0" => map (S. _ 2) = parsevalue (S. _ 3); Case "1" => map (S. _ 2) = parsenode (lex); Case "2" => map (S. _ 2) = S. _ 3; Case "-1" => return SMAP. empty ++ map;} s = Lex. next;} while (s! = NULL); Return SMAP. empty ++ map;} private def numbertype (S: string): Int = {If (S = NULL) Return-1; Val n = S. length; If (n = 0) Return-1; Val K = array (0, 0, 0, 0, 0); For (I <-s. getbytes) {I match {Case 43 | 45 => K (0) = I; K (1) + = 1; Case 46 => K (2) = I; K (3) = K (3) + 1; Case 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 => K (4) = K (4) + 1; Case _ => return-1 ;}} if (K (1)> 0 & (K (1)> 1 | K (0)> 0 | n <2) Return-1; if (K (3)> 0) return if (K (3) <= 1 & K (2)> = 1 & K (2) <= N-2) 3 else-1; if (K (4)> 9) return 2; return 1 ;}} class XML (Private Val S: string) {private Val A: array [int] = array (0, 0, 0); Private def subs (F: String, S: int, E: INT) = F. substring (S, e); Private def idx (F: String, E: Char, S: INT) = F. indexof (E, S); Private def next: tuple3 [String, String, string] = {A (0) = idx (S, '<', A (1 )); if (A (0) =-1) return NULL; S. charat (A (0) + 1) Match {case '? '=> A (1) = idx (S,'> ', a (0); return next; Case'/'=> A (1) = idx (S, '>', a (0); Return ("-1", subs (s, A (0) + 2, A (1 )),""); case _ =>} A (2) = idx (S, '<', a (0) + 1); S. charat (A (2) + 1) Match {Case '/' => A (1) = idx (S, '>', a (2); a (3) = idx (S, '>', a (0); Return ("0", subs (s, A (0) + 1, A (3 )), subs (s, A (3) + 1, idx (S, '<', a (0) + 1); Case '! '=> A (3) = S. indexof ("]>", A (2) + 1); A (1) = idx (S, '>', A (3) + 3 ); return ("2", subs (s, A (0) + 1, idx (S, '>', a (0), subs (s, A (2) + 9, A (3); Case _ => A (1) = idx (S, '>', a (0); Return ("1 ", subs (s, A (0) + 1, A (1 )),"");}}}
Scala XML Parser