-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXML.java
More file actions
150 lines (141 loc) · 3.68 KB
/
XML.java
File metadata and controls
150 lines (141 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import java.util.*;
import java.io.File;
public class XML {
String xml = "";
ArrayList<String> token;
public XML(String xml) {
this.xml = xml;
this.token = new ArrayList<String>();
}
public String parseXML() {
xml = xml.trim();
if(xml.length() == 0) {
return "";
}
else if(xml.charAt(0)=='<' && xml.charAt(1) != '/') {
String st = startTag();
//token.add(st);
String r = parseXML();
boolean result = endTag(st);
String r1 = parseXML();
if(!result || r1==null || r == null) {
System.out.println("Invalid XML!!!");
return null;
}
if(r1.length()>0) {
return "{'"+st+"':"+r+","+r1.substring(1, r1.length()-1)+"}";
}
else {
return "{'"+st+"':"+r+"}";
}
}
else{
String content = content();
//token.add(content);
return content;
}
}
public String content() {
int e = xml.indexOf("<");
String result = xml.substring(0, e);
if(!result.equals("")) {
if(!(isBoolean(result) || isNumber(result))) {
result = "'" + result + "'";
}
token.add(result);
}
xml = xml.substring(e);
return result;
}
public boolean isBoolean(String str) {
return str.equalsIgnoreCase("true") || str.equalsIgnoreCase("false");
}
public boolean isNumber(String str) {
try {
Double.parseDouble(str);
}
catch(Exception e) {
return false;
}
return true;
}
public String startTag() {
int s = xml.indexOf("<") + 1;
int e = xml.indexOf(">");
String result = xml.substring(s, e);
xml= xml.substring(e+1);
token.add(result);
return result;
}
public boolean endTag(String start) {
int s = xml.indexOf("<") + 1;
int e = xml.indexOf(">");
if(e<0 || s<0) {
return false;
}
String tag = xml.substring(s+1, e);
boolean result = xml.charAt(s) == '/' && start.equalsIgnoreCase(tag);
xml = xml.substring(e+1);
return result;
}
public static void main(String args[]) {
//Reading input from the file.
System.out.println("Input: ");
System.out.println("----------------------------------------------------------------------");
String input = "";
try{
File file = new File(".\\input.txt");
Scanner scan = new Scanner(file);
String line = "";
while(scan.hasNextLine()){
line =scan.nextLine();
if(!line.equals("")){
System.out.println(line);
input +=line;
}
}
}catch(Exception e){
System.out.println("File could not Loaded!!");
}
System.out.println("----------------------------------------------------------------------");
//<A> <F> </F><D>true</D><C>abc</C><B>Text</B><E>23</E></A><A><D>true</D><C>abc</C><B>Text</B><E>23</E></A>
XML file = new XML(input);
String output = file.parseXML();
System.out.println("Tokens : "+file.token.toString());
System.out.println("----------------------------------------------------------------------");
System.out.println(format(output));
System.out.println("----------------------------------------------------------------------");
}
public static String format(String str) {
if(str == null) {
System.out.print("NOT Valid JSON");
return "";
}
String result = "";
int tabs = 0;
for(int i=0; i<str.length(); i++) {
if(str.charAt(i) == '{') {
tabs++;
result += "{\n";
for(int j=0; j<tabs; j++)
result += "\t";
}
else if(str.charAt(i)=='}'){
result+="\n";
tabs--;
for(int j=0; j<tabs; j++)
result += "\t";
result += "}";
}
else if(str.charAt(i) == ',') {
result += ",\n";
for(int j=0; j<tabs; j++)
result += "\t";
}
else {
result += str.charAt(i);
}
}
return result;
}
}