//读取xml文件 InputStream in = new FileInputStream(basePath+"/ajax.xml"); byte[] b = new byte[1024]; int length = 0; StringBuffer sb = new StringBuffer(); while ((length = in.read(b)) != -1) { //以前在这出现乱码问题,后来在这设置了编码格式 sb.append(new String(b, 0, length,"UTF-8")); } in.close(); //转换为json字符串 String jsonStr = sb.substring(sb.indexOf("{"), sb.lastIndexOf("}") + 1); JSONArray ja = JSONArray.fromObject("[" + jsonStr + "]"); for (int i = 0; i < ja.size(); i++) { String number=ja.getJSONObject(i).get("text").toString().split(" ")[0].toString().trim(); String numberValue=ja.getJSONObject(i).get("text").toString(); if(number.equals(codeId)){ return numberValue.substring(numberValue.indexOf(" "),numberValue.lastIndexOf(" ") + 1); } }
?//获取项web项目根目录
String basePath = request.getContextPath();
?
XML格式:
<?xml version="1.0" standalone="yes" ?> <neverModules> <autocomplete> <dataSource> <![CDATA[ [ {text:'000000 大盘走势 DPZS', content:'000000'}, {text:'000001 平安银行 PAYX', content:'000001'}, {text:'000000 系统代码 xtdm', content:'系统代码'} ] ]]></dataSource> </autocomplete></neverModules>
?
?
System.out.println("大盘走势");