Skip to content

Commit fbb6c40

Browse files
committed
add XMLUtil
1 parent 328d72b commit fbb6c40

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

lib/utils/CSVUtil.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.FileWriter;
44
import java.nio.file.Path;
5+
import java.util.ArrayList;
56
import java.util.List;
67
import java.util.stream.Collectors;
78

@@ -67,4 +68,94 @@ public static void write2DArray2Csv(List<List<String>> strList, Path filePath) {
6768
}
6869
}
6970

71+
/**
72+
* read csv line by line and parse single line to this function code example
73+
* String csvFile = "/Users/mkyong/csv/country2.csv";
74+
*
75+
* Scanner scanner = new Scanner(new File(csvFile)); while
76+
* (scanner.hasNext()) { List<String> line = parseLine(scanner.nextLine());
77+
* System.out.println("Country [id= " + line.get(0) + ", code= " +
78+
* line.get(1) + " , name=" + line.get(2) + "]"); } scanner.close();
79+
*
80+
* @param cvsLine
81+
* @param separator
82+
* @param quote
83+
* @return
84+
*/
85+
public static List<String> parseLine(String cvsLine, char separator, char quote) {
86+
87+
List<String> result = new ArrayList<>();
88+
89+
// if empty, return!
90+
if (cvsLine == null || cvsLine.isEmpty()) {
91+
return result;
92+
}
93+
94+
StringBuffer curVal = new StringBuffer();
95+
boolean inQuotes = false;
96+
boolean startCollectChar = false;
97+
boolean doubleQuotesInColumn = false;
98+
99+
char[] chars = cvsLine.toCharArray();
100+
101+
for (char ch : chars) {
102+
103+
if (inQuotes) {
104+
startCollectChar = true;
105+
if (ch == quote) {
106+
inQuotes = false;
107+
doubleQuotesInColumn = false;
108+
} else {
109+
110+
// Fixed : allow "" in custom quote enclosed
111+
if (ch == '\"') {
112+
if (!doubleQuotesInColumn) {
113+
curVal.append(ch);
114+
doubleQuotesInColumn = true;
115+
}
116+
} else {
117+
curVal.append(ch);
118+
}
119+
120+
}
121+
} else {
122+
if (ch == quote) {
123+
124+
inQuotes = true;
125+
126+
// Fixed : allow "" in empty quote enclosed
127+
if (chars[0] != '"' && quote == '\"') {
128+
curVal.append('"');
129+
}
130+
131+
// double quotes in column will hit this!
132+
if (startCollectChar) {
133+
curVal.append('"');
134+
}
135+
136+
} else if (ch == separator) {
137+
138+
result.add(curVal.toString());
139+
140+
curVal = new StringBuffer();
141+
startCollectChar = false;
142+
143+
} else if (ch == '\r') {
144+
// ignore LF characters
145+
continue;
146+
} else if (ch == '\n') {
147+
// the end, break!
148+
break;
149+
} else {
150+
curVal.append(ch);
151+
}
152+
}
153+
154+
}
155+
156+
result.add(curVal.toString());
157+
158+
return result;
159+
}
160+
70161
}

lib/utils/XMLUtil.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package javaToolkit.lib.utils;
22

33
import java.io.StringWriter;
4+
import java.util.ArrayList;
5+
import java.util.List;
46

57
import javax.xml.transform.*;
68
import javax.xml.transform.dom.DOMSource;
79
import javax.xml.transform.stream.StreamResult;
10+
811
import org.w3c.dom.Node;
912

1013
public class XMLUtil {
@@ -22,4 +25,15 @@ public static String nodeToString(Node node) {
2225
return sw.toString();
2326
}
2427

28+
public static List<Node> getChildbyTagName(Node parent, String nodeName) {
29+
List<Node> eleList = new ArrayList<Node>();
30+
for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
31+
// System.out.println("node name " + child.getNodeName());
32+
if (nodeName.equals(child.getNodeName())) {
33+
eleList.add(child);
34+
}
35+
}
36+
return eleList;
37+
}
38+
2539
}

0 commit comments

Comments
 (0)