-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXmlWriter.java
121 lines (119 loc) · 3.99 KB
/
XmlWriter.java
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
import java.io.File;
import java.util.Map;
import java.util.Stack;
import java.io.FileWriter;
import java.io.BufferedWriter;
public class XmlWriter {
Stack<String> elements;
StringBuilder sb;
int currentIndent;
public XmlWriter(){
currentIndent = 0;
elements = new Stack<>();
sb = new StringBuilder();
}
public XmlWriter(Stack<String> elements){
elements = elements;
currentIndent = elements.size();
sb = new StringBuilder();
}
public void writeStartDocument(String encoding){
sb.append("<?xml version=\"1.0\" encoding=\"").append(encoding).append("\" ?>\n");
}
public void writeStartDocument(){
sb.append("<?xml version=\"1.0\" ?>\n");
}
public void writeStartElement(String elementName){
int indent = currentIndent;
while(indent>0){
sb.append("\t");
indent--;
}
sb.append("<").append(elementName).append(">\n");
elements.push(elementName);
currentIndent++;
}
public void writeStartElementWithAttr(String elementName, Map<String,String> attributes){
int indent = currentIndent;
while(indent>0){
sb.append("\t");
indent--;
}
sb.append("<").append(elementName);
for(Map.Entry attribute: attributes.entrySet()){
sb.append(" ").append(attribute.getKey()).append("=\"").append(attribute.getValue()).append("\"");
}
sb.append(">\n");
elements.push(elementName);
currentIndent++;
}
public void writeStartEndElement( String elementName,String data){
int indent = currentIndent;
while(indent>0){
sb.append("\t");
indent--;
}
sb.append("<").append(elementName).append(">").append(data).append("</").append(elementName).append(">\n");
}
public void writeStartEndElementWithAttr( String elementName,String data,Map<String,String> attributes){
int indent = currentIndent;
while(indent>0){
sb.append("\t");
indent--;
}
sb.append("<").append(elementName);
for(Map.Entry attribute: attributes.entrySet()){
sb.append(" ").append(attribute.getKey()).append("=\"").append(attribute.getValue()).append("\"");
}
sb.append(">").append(data).append("</").append(elementName).append(">\n");
}
public void writeEndElement() throws XmlwriterException {
if(currentIndent == 0){
throw new XmlwriterException("No start element found.");
}else {
int indent = currentIndent-1;
String elementName = elements.pop();
while(indent>0){
sb.append("\t");
indent--;
}
sb.append("</").append(elementName).append(">\n");
currentIndent--;
}
}
public StringBuilder getDocument(){
return this.sb;
}
public DocumentElements getDocumentElements(){
DocumentElements documentElements = new DocumentElements();
documentElements.setStringBuilder(sb);
documentElements.setElements(elements);
return documentElements;
}
public boolean SaveDocument(String location,String fileName){
File file;
try{
if(location.charAt(location.length()-1) == '\\'){
file = new File(location + fileName + ".xml");
}else{
file = new File(location +"\\"+ fileName + ".xml");
}
FileWriter fr = new FileWriter(file, false);
BufferedWriter bw = new BufferedWriter(fr);
bw.write(this.sb.toString());
bw.close();
fr.close();
sb = new StringBuilder();
return true;
}catch (Exception e){
System.out.println(e.getMessage());
return false;
}
}
class XmlwriterException extends Exception{
XmlwriterException (String str)
{
super(str);
}
}
}