使用Java解析xml文件为json格式
2018-01-08 19:22
771 查看
解析结果:{{"type":"FUNCTION","name":"F_EQ","returnValue":"BOOL"}, [[{"anno":"aaaaaaaaaaaaaa","keyword":"VAR","name":"D","pointer":true,"retain":"CONSTANT","type":"REF_TO","value":"111"}],[{"anno":"BBBBBBBBBBB","keyword":"VAR_INPUT","name":"A","pointer":false,"retain":"RETAIN","type":"FLOAT","value":""},{"anno":"CCCCCCCC","keyword":"VAR_INPUT","name":"G","pointer":false,"retain":"RETAIN","type":"FLOAT","value":""},{"anno":"FFFFFFFFFFF","keyword":"VAR_INPUT","name":"H","pointer":false,"retain":"RETAIN","type":"FLOAT","value":""}],[{"anno":"","keyword":"VAR_OUTPUT","name":"B","pointer":false,"retain":"NON_RETAIN","type":"FLOAT","value":""}],[{"anno":"","keyword":"VAR_IN_OUT","name":"C","pointer":false,"retain":"PERSISTENT","type":"FLOAT","value":""}],[{"anno":"","keyword":"VAR_EXTERNAL","name":"E","pointer":false,"retain":"","type":"INT","value":""}],[{"anno":"","keyword":"VAR_TEMP","name":"F","pointer":false,"retain":"","type":"INT","value":""}]],
{"代码":"IF g_byMO_MI_Num < 1 THEN;ELSEFOR i := 0 TO g_byMO_MI_Num -1 DOIF u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi <>0 THENIF ( u_apMOMITbl[i]^.Axis.output_mi.byCurrMotionID = MOTIONID_STOP_HYB ) THENu_apMOMITbl[i]^.MIInterface.pFB_MI_Stop_HYB^(xExecute\t:=
TRUE,//xOpenLoop\t:= TRUE,Axis_mi_ref\t:= u_apMOMITbl[i]^.Axis);END_IFIF u_apMOMITbl[i]^.Axis.output_mi.rOutVelocity < 0 THENDev_Output_Write(OUTPUT_DRIVER1_NEGATIVE,1);Dev_PowerGroup_WriteSpeed(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,-u_apMOMITbl[i]^.Axis.output_mi.rOutVelocity);ELSEDev_Output_Write(OUTPUT_DRIVER1_NEGATIVE,0);Dev_PowerGroup_WriteSpeed(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rOutVelocity);END_IFDev_PowerGroup_WritePress(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rOutPressure);//Dev_PowerGroup_WritePress(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rProfilePressure);//Dev_PowerGroup_WriteSpeed(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rProfileSpeed);//Dev_PowerGroup_WritePress(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rOutPressure);//Dev_PowerGroup_WriteSpeed(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rOutVelocity);Dev_PowerGroup_WriteServoValve(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rOutServoValveSpeed);Dev_PowerGroup_WriteTorque(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rOutTorque);Dev_PowerGroup_WritePosition(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rOutPosition);END_IFEND_FOREND_IFEND_FUNCTION"}}
1,这是我要解析的xml文件
FUNCTION F_EQ : BOOL
VAR CONSTANT
//aaaaaaaaaaaaaa
D : REF_TO DWORD := 111 ; //dhdh dsad a
END_VAR
VAR_INPUT RETAIN
//BBBBBBBBBBB
A : FLOAT ;
//CCCCCCCC
G : FLOAT ;
//FFFFFFFFFFF
H : FLOAT ;
END_VAR
VAR_OUTPUT NON_RETAIN
B : FLOAT;
END_VAR
VAR_IN_OUT PERSISTENT
C : FLOAT;
END_VAR
VAR_EXTERNA
4000
L
E : INT ;
END_VAR
VAR_TEMP
F : INT ;
END_VAR
IF g_byMO_MI_Num < 1 THEN;
ELSE
FOR i := 0 TO g_byMO_MI_Num -1 DO
IF u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi <>0 THEN
IF ( u_apMOMITbl[i]^.Axis.output_mi.byCurrMotionID = MOTIONID_STOP_HYB ) THEN
u_apMOMITbl[i]^.MIInterface.pFB_MI_Stop_HYB^(
xExecute
:= TRUE,
//xOpenLoop
:= TRUE,
Axis_mi_ref
:= u_apMOMITbl[i]^.Axis
);
END_IF
IF u_apMOMITbl[i]^.Axis.output_mi.rOutVelocity < 0 THEN
Dev_Output_Write(OUTPUT_DRIVER1_NEGATIVE,1);
Dev_PowerGroup_WriteSpeed(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,-u_apMOMITbl[i]^.Axis.output_mi.rOutVelocity);
ELSE
Dev_Output_Write(OUTPUT_DRIVER1_NEGATIVE,0);
Dev_PowerGroup_WriteSpeed(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rOutVelocity);
END_IF
Dev_PowerGroup_WritePress(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rOutPressure);
//Dev_PowerGroup_WritePress(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rProfilePressure);
//Dev_PowerGroup_WriteSpeed(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rProfileSpeed);
//Dev_PowerGroup_WritePress(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rOutPressure);
//Dev_PowerGroup_WriteSpeed(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rOutVelocity);
Dev_PowerGroup_WriteServoValve(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rOutServoValveSpeed);
Dev_PowerGroup_WriteTorque(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rOutTorque);
Dev_PowerGroup_WritePosition(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rOutPosition);
END_IF
END_FOR
END_IF
END_FUNCTION
2、需要定义两个类存储这个文件中的对象
这个文件有两个对象,整个文件为一个对象,其中的变量的属性为一个对象
package plc;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class Json {
private JSONObject jsonObject;
private JSONArray jsonArry;
private JSONObject code;
public Json() {
super();
}
public Json(JSONObject jsonObject, JSONArray jsonArry, JSONObject code) {
super();
this.jsonObject = jsonObject;
this.jsonArry = jsonArry;
this.code = code;
}
public JSONObject getJsonObject() {
return jsonObject;
}
public void setJsonObject(JSONObject jsonObject) {
this.jsonObject = jsonObject;
}
public JSONArray getJsonArry() {
return jsonArry;
}
public void setJsonArry(JSONArray jsonArry) {
this.jsonArry = jsonArry;
}
public JSONObject getCode() {
return code;
}
public void setCode(JSONObject code) {
this.code = code;
}
@Override
public String toString() {
return "{" + jsonObject + ", " + jsonArry + ", " + code + "}";
}
}
package plc;
public class Parameter {
private String keyword;
private String name;
private String retain;
private String type;
private boolean pointer;
private String value;
private String anno;
public Parameter() {
super();
}
public Parameter(String keyword, String name, String retain, String type, boolean pointer, String value,
String anno) {
super();
this.keyword = keyword;
this.name = name;
this.retain = retain;
this.type = type;
this.pointer = pointer;
this.value = value;
this.anno = anno;
}
public String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRetain() {
return retain;
}
public void setRetain(String retain) {
this.retain = retain;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isPointer() {
return pointer;
}
public void setPointer(boolean pointer) {
this.pointer = pointer;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getAnno() {
return anno;
}
public void setAnno(String anno) {
this.anno = anno;
}
@Override
public String toString() {
return "[keyword:" + keyword +", retain:" + retain + ", name:" + name + ", type:" + type
+ ", pointer:" + pointer + ", value:" + value + ", anno:" + anno + "]";
}
}
3、解析文件的主代码
package plc;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Pattern;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class TManalysis3 {
private static Json json;
public static void main(String[] args) throws IOException {
json = new Json();
JSONArray jsonArray = new JSONArray();
json.setJsonArry(jsonArray);
File file = new File("E:\\JAV\\WorkSpace\\WorkSpace-rcp\\plc111\\plc.st");
InputStreamReader read = new InputStreamReader(new FileInputStream(file));
BufferedReader bufferedReader = new BufferedReader(read);
String regex = "\\s";
String regexFunction = "[A-Z]+\\s[A-Za-z0-9_]+\\s:\\s[A-Za-z0-9_]+";
String regexVar = "(VAR.*|VAR)\\s[A-Z_]+";
String regexVar1 = "^VAR.*";
String regexArgument1 = "[A-Z]+\\s:\\s[A-Z]+\\s;";
String regexArgument2 = "[A-Z]+\\s:\\s[A-Z]+;";
String regexPointer = "[A-Z]+\\s:\\s[A-Z_]+\\s[A-Z]+\\s:=\\s[0-9]+\\s;";
String regexAnnotation = "\\//[A-Za-z0-9_\u0391-\uFFE5]+";
String misTake = "[\u4e00-\u9fa5]";
Pattern pattern = Pattern.compile(regex);
String lineStri = null;
String annotation = null;
String s = new String();
JSONArray js = new JSONArray();
Parameter p = new Parameter();
while ((lineStri = bufferedReader.readLine()) != null) {
String lineStr = lineStri.replaceAll(" {2,}", " ");
lineStr = lineStr.trim();
String[] lineWords = pattern.split(lineStr);
if(lineStr.matches(regexAnnotation)) {
}
for (int i = 0; i < lineWords.length; i++) {
if (lineStr.matches(regexFunction)) {
JSONObject jsobject = new JSONObject();
json.setJsonObject(jsobject);
jsobject.put("type", lineWords[0]);
jsobject.put("name", lineWords[1]);
jsobject.put("returnValue", lineWords[3]);
break;
} else if (lineStr.matches(regexAnnotation)) {
annotation = lineStr.substring(2);
break;
} else if (lineStr.matches(regexVar)) {
p.setKeyword(lineWords[i]);
p.setRetain(lineWords[i + 1]);
break;
} else if (lineStr.matches(regexVar1)) {
p.setKeyword(lineWords[i]);
p.setRetain(null);
break;
}
else if (lineWords[i].matches("END_VAR")) {
jsonArray.add(js);
p = new Parameter();
js = new JSONArray();
break;
} else if (lineStr.matches(regexPointer)) {
p.setName(lineWords[0]);
p.setPointer(true);
p.setType(lineWords[2]);
p.setValue(lineWords[5]);
p.setAnno(annotation);
annotation = null;
js.add(p);
break;
} else if (lineStr.matches(regexArgument1)) {
p.setName(lineWords[0]);
p.setType(lineWords[2]);
p.setAnno(annotation);
annotation = null;
js.add(p);
break;
} else if (lineStr.matches(regexArgument2)) {
p.setName(lineWords[0]);
p.setType(lineWords[2].substring(0, lineWords[2].length() - 1));
p.setAnno(annotation);
annotation = null;
js.add(p);
break;
} else if (lineStr.matches(misTake)) {
System.out.println("无法解析");
System.exit(0);
} else {
// System.out.println(lineStr);
s = s + lineStr;
break;
}
}
}
JSONObject jsonCODE = new JSONObject();
json.setCode(jsonCODE);
jsonCODE.put("代码", s);
System.out.println(json);
}
}
{"代码":"IF g_byMO_MI_Num < 1 THEN;ELSEFOR i := 0 TO g_byMO_MI_Num -1 DOIF u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi <>0 THENIF ( u_apMOMITbl[i]^.Axis.output_mi.byCurrMotionID = MOTIONID_STOP_HYB ) THENu_apMOMITbl[i]^.MIInterface.pFB_MI_Stop_HYB^(xExecute\t:=
TRUE,//xOpenLoop\t:= TRUE,Axis_mi_ref\t:= u_apMOMITbl[i]^.Axis);END_IFIF u_apMOMITbl[i]^.Axis.output_mi.rOutVelocity < 0 THENDev_Output_Write(OUTPUT_DRIVER1_NEGATIVE,1);Dev_PowerGroup_WriteSpeed(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,-u_apMOMITbl[i]^.Axis.output_mi.rOutVelocity);ELSEDev_Output_Write(OUTPUT_DRIVER1_NEGATIVE,0);Dev_PowerGroup_WriteSpeed(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rOutVelocity);END_IFDev_PowerGroup_WritePress(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rOutPressure);//Dev_PowerGroup_WritePress(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rProfilePressure);//Dev_PowerGroup_WriteSpeed(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rProfileSpeed);//Dev_PowerGroup_WritePress(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rOutPressure);//Dev_PowerGroup_WriteSpeed(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rOutVelocity);Dev_PowerGroup_WriteServoValve(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rOutServoValveSpeed);Dev_PowerGroup_WriteTorque(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rOutTorque);Dev_PowerGroup_WritePosition(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rOutPosition);END_IFEND_FOREND_IFEND_FUNCTION"}}
1,这是我要解析的xml文件
FUNCTION F_EQ : BOOL
VAR CONSTANT
//aaaaaaaaaaaaaa
D : REF_TO DWORD := 111 ; //dhdh dsad a
END_VAR
VAR_INPUT RETAIN
//BBBBBBBBBBB
A : FLOAT ;
//CCCCCCCC
G : FLOAT ;
//FFFFFFFFFFF
H : FLOAT ;
END_VAR
VAR_OUTPUT NON_RETAIN
B : FLOAT;
END_VAR
VAR_IN_OUT PERSISTENT
C : FLOAT;
END_VAR
VAR_EXTERNA
4000
L
E : INT ;
END_VAR
VAR_TEMP
F : INT ;
END_VAR
IF g_byMO_MI_Num < 1 THEN;
ELSE
FOR i := 0 TO g_byMO_MI_Num -1 DO
IF u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi <>0 THEN
IF ( u_apMOMITbl[i]^.Axis.output_mi.byCurrMotionID = MOTIONID_STOP_HYB ) THEN
u_apMOMITbl[i]^.MIInterface.pFB_MI_Stop_HYB^(
xExecute
:= TRUE,
//xOpenLoop
:= TRUE,
Axis_mi_ref
:= u_apMOMITbl[i]^.Axis
);
END_IF
IF u_apMOMITbl[i]^.Axis.output_mi.rOutVelocity < 0 THEN
Dev_Output_Write(OUTPUT_DRIVER1_NEGATIVE,1);
Dev_PowerGroup_WriteSpeed(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,-u_apMOMITbl[i]^.Axis.output_mi.rOutVelocity);
ELSE
Dev_Output_Write(OUTPUT_DRIVER1_NEGATIVE,0);
Dev_PowerGroup_WriteSpeed(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rOutVelocity);
END_IF
Dev_PowerGroup_WritePress(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rOutPressure);
//Dev_PowerGroup_WritePress(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rProfilePressure);
//Dev_PowerGroup_WriteSpeed(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rProfileSpeed);
//Dev_PowerGroup_WritePress(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rOutPressure);
//Dev_PowerGroup_WriteSpeed(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rOutVelocity);
Dev_PowerGroup_WriteServoValve(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rOutServoValveSpeed);
Dev_PowerGroup_WriteTorque(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rOutTorque);
Dev_PowerGroup_WritePosition(u_apMOMITbl[i]^.Axis.input_mi.pConfig_mi^.byPowerGroupIndex,u_apMOMITbl[i]^.Axis.output_mi.rOutPosition);
END_IF
END_FOR
END_IF
END_FUNCTION
2、需要定义两个类存储这个文件中的对象
这个文件有两个对象,整个文件为一个对象,其中的变量的属性为一个对象
package plc;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class Json {
private JSONObject jsonObject;
private JSONArray jsonArry;
private JSONObject code;
public Json() {
super();
}
public Json(JSONObject jsonObject, JSONArray jsonArry, JSONObject code) {
super();
this.jsonObject = jsonObject;
this.jsonArry = jsonArry;
this.code = code;
}
public JSONObject getJsonObject() {
return jsonObject;
}
public void setJsonObject(JSONObject jsonObject) {
this.jsonObject = jsonObject;
}
public JSONArray getJsonArry() {
return jsonArry;
}
public void setJsonArry(JSONArray jsonArry) {
this.jsonArry = jsonArry;
}
public JSONObject getCode() {
return code;
}
public void setCode(JSONObject code) {
this.code = code;
}
@Override
public String toString() {
return "{" + jsonObject + ", " + jsonArry + ", " + code + "}";
}
}
package plc;
public class Parameter {
private String keyword;
private String name;
private String retain;
private String type;
private boolean pointer;
private String value;
private String anno;
public Parameter() {
super();
}
public Parameter(String keyword, String name, String retain, String type, boolean pointer, String value,
String anno) {
super();
this.keyword = keyword;
this.name = name;
this.retain = retain;
this.type = type;
this.pointer = pointer;
this.value = value;
this.anno = anno;
}
public String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRetain() {
return retain;
}
public void setRetain(String retain) {
this.retain = retain;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isPointer() {
return pointer;
}
public void setPointer(boolean pointer) {
this.pointer = pointer;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getAnno() {
return anno;
}
public void setAnno(String anno) {
this.anno = anno;
}
@Override
public String toString() {
return "[keyword:" + keyword +", retain:" + retain + ", name:" + name + ", type:" + type
+ ", pointer:" + pointer + ", value:" + value + ", anno:" + anno + "]";
}
}
3、解析文件的主代码
package plc;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Pattern;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class TManalysis3 {
private static Json json;
public static void main(String[] args) throws IOException {
json = new Json();
JSONArray jsonArray = new JSONArray();
json.setJsonArry(jsonArray);
File file = new File("E:\\JAV\\WorkSpace\\WorkSpace-rcp\\plc111\\plc.st");
InputStreamReader read = new InputStreamReader(new FileInputStream(file));
BufferedReader bufferedReader = new BufferedReader(read);
String regex = "\\s";
String regexFunction = "[A-Z]+\\s[A-Za-z0-9_]+\\s:\\s[A-Za-z0-9_]+";
String regexVar = "(VAR.*|VAR)\\s[A-Z_]+";
String regexVar1 = "^VAR.*";
String regexArgument1 = "[A-Z]+\\s:\\s[A-Z]+\\s;";
String regexArgument2 = "[A-Z]+\\s:\\s[A-Z]+;";
String regexPointer = "[A-Z]+\\s:\\s[A-Z_]+\\s[A-Z]+\\s:=\\s[0-9]+\\s;";
String regexAnnotation = "\\//[A-Za-z0-9_\u0391-\uFFE5]+";
String misTake = "[\u4e00-\u9fa5]";
Pattern pattern = Pattern.compile(regex);
String lineStri = null;
String annotation = null;
String s = new String();
JSONArray js = new JSONArray();
Parameter p = new Parameter();
while ((lineStri = bufferedReader.readLine()) != null) {
String lineStr = lineStri.replaceAll(" {2,}", " ");
lineStr = lineStr.trim();
String[] lineWords = pattern.split(lineStr);
if(lineStr.matches(regexAnnotation)) {
}
for (int i = 0; i < lineWords.length; i++) {
if (lineStr.matches(regexFunction)) {
JSONObject jsobject = new JSONObject();
json.setJsonObject(jsobject);
jsobject.put("type", lineWords[0]);
jsobject.put("name", lineWords[1]);
jsobject.put("returnValue", lineWords[3]);
break;
} else if (lineStr.matches(regexAnnotation)) {
annotation = lineStr.substring(2);
break;
} else if (lineStr.matches(regexVar)) {
p.setKeyword(lineWords[i]);
p.setRetain(lineWords[i + 1]);
break;
} else if (lineStr.matches(regexVar1)) {
p.setKeyword(lineWords[i]);
p.setRetain(null);
break;
}
else if (lineWords[i].matches("END_VAR")) {
jsonArray.add(js);
p = new Parameter();
js = new JSONArray();
break;
} else if (lineStr.matches(regexPointer)) {
p.setName(lineWords[0]);
p.setPointer(true);
p.setType(lineWords[2]);
p.setValue(lineWords[5]);
p.setAnno(annotation);
annotation = null;
js.add(p);
break;
} else if (lineStr.matches(regexArgument1)) {
p.setName(lineWords[0]);
p.setType(lineWords[2]);
p.setAnno(annotation);
annotation = null;
js.add(p);
break;
} else if (lineStr.matches(regexArgument2)) {
p.setName(lineWords[0]);
p.setType(lineWords[2].substring(0, lineWords[2].length() - 1));
p.setAnno(annotation);
annotation = null;
js.add(p);
break;
} else if (lineStr.matches(misTake)) {
System.out.println("无法解析");
System.exit(0);
} else {
// System.out.println(lineStr);
s = s + lineStr;
break;
}
}
}
JSONObject jsonCODE = new JSONObject();
json.setCode(jsonCODE);
jsonCODE.put("代码", s);
System.out.println(json);
}
}
相关文章推荐
- java中采用集合使用pull解析xml文件格式的方法
- 百度天气api开发 全解析xml和json格式 使用json-lib jsonObject、 Gson 方式把json 转换成 java对象
- java将xml解析,然后将其转换为json格式(②)
- java使用dom4j解析xml文件
- 在Go语言程序中使用gojson来解析JSON格式文件
- java使用dom4j解析xml配置文件实现抽象工厂反射示例
- 在VS.NET2005中使用java代码段以及SOL文件格式的解析
- Java进阶(二十七)使用Dom4j解析XML文件
- Java 语言对XMl 格式文件的 纯解析
- java使用Xstream注解 解析xml文件 并将数据导入MySql数据库中
- perl 解析xml ,json文件及curl工具的使用
- Java&Xml教程(六)使用JDOM解析XML文件
- Java&Xml教程(六)使用JDOM解析XML文件
- java解析json格式文件
- Java乔晓松-使用SAX,DOM,pull解析XML文件以及pull解析生成xml文件
- iOS开发中 使用XML和JSON对本地文件进行解析
- java网络编程--解析通过web.xml文件控制利用过滤器实现编码格式步骤
- Android访问网络:服务端返回XML或JSON格式数据,Android 进行解析并使用ListView显示
- Java使用DOM解析XML文件
- [Java学习]使用digester 解析xml配置文件