我试图解析XML文件并将数据写入CSV文件中,而仅通过纯Java不使用DOM或SAX解析器。
输出应采用以下格式
姓名| 年龄| 名称 日期| 家乡
史蒂夫(Steve),21岁,员工,2013年7月14日,加利福尼亚
约翰,24岁,店员,2014年10月17日,纽约
Phil,45岁,校长,2015年8月20日,库比蒂诺
我目前的输出格式
TotalStudent = 3
家长资料:student1
名字=米克
年龄= 21
指定=学生
日期= 07/10/2015
家乡=金奈
家长资料:student2
名称=史蒂夫
年龄= 22
名称=员工
日期= 07/10/2015
家乡= Cupertino
家长资料:student3
名称=史蒂夫
年龄= 22
名称=员工
日期= 07/15/2015
家乡= Cupertino
家长资料:student4
名称=史蒂夫
年龄= 22
名称=员工
日期= 07/18/2015
家乡= Cupertino
读取标记中的“员工总数”的XML检查时,如果总数为3,而xml标记总数也为三个(即,“员工详细信息”总数标签),则这三个员工详细信息将以上述指定格式写入CSV文件中和CSV(日期和时间明智的文件名)文件,否则需要创建错误日志。
我无法以这种格式打印输出。请帮助我解决这个问题,代码还应该是O(n)时间复杂度和O(1)空间复杂度。如果您粘贴完整的解决方案而不是方法,这对我会有所帮助,
提前致谢。
这是我的代码
public class Solution1 {
public static ArrayList<String> attri = new ArrayList<String>();
public static ArrayList<String> value = new ArrayList<String>();
public static ArrayList<String> parent = new ArrayList<String>();
public static void main(String[] args) {
try {
File xmlfile = new File("/Users/mani/Documents/workspace/XMLReaderWithOutParser/Sample.xml");
FileReader fr = new FileReader(xmlfile);
BufferedReader bf = new BufferedReader(fr);
String xml_data = "";
int nol = 0;
StringBuilder sb = new StringBuilder();
while ((xml_data = bf.readLine()) != null) {
nol = nol + 1;
sb.append(xml_data);
}
String data = sb.toString();
for (int i = 0; i <= data.length(); i++) {
int end = data.indexOf(">");
int start = data.indexOf("<");
String attribute_data = data.substring(start + 1, end);
data = data.substring(end + 1);
String data_dummy = data;
int find = data.indexOf("</" + attribute_data + ">");
if (find > -1) {
if (data_dummy.startsWith("<")) {
parent.add(attribute_data);
} else {
if (attribute_data.startsWith("/")) {
} else {
attri.add(attribute_data);
}
}
}
if (data.length() > 0) {
if (data.startsWith("<")) {
} else {
int filter = data.indexOf("<");
if (filter > -1) {
String split_value = data.substring(0, filter);
value.add(split_value);
System.out.println(attribute_data + "=" + split_value);
}
}
}
}
} catch (Exception e) {
System.out.println("Error is :" + e.toString());
}
}
}
样本XML文件
<Employee>
<TotalEmployeeNo>3</TotalEmployeeNo>
<person1>
<Name>mick</Name><Age>21</Age><Designation>student</Designation><Date>07/10/2015</Date>
<Hometown>
Chennai</Hometown>
</person1>
<person2><Name>Steve</Name><Age>20</Age><Designation>staff</Designation><Date>07/10/2015</Date>
<Hometown>Cupertino</Hometown>
</person2>
<person3><Name>Steve</Name><Age>29</Age><Designation>Clerk</Designation><Date>07/15/2015</Date>
<Hometown>Cupertino</Hometown>
</person3>
<person4><Name>Steve</Name><Age>25</Age><Designation>staff</Designation><Date>07/18/2015</Date>
<Hometown>Cupertino</Hometown>
</person4>
</Employee>
首先,手动解析XML只是吸引错误和陷阱!
由于您必须在没有标准解析器的情况下执行此操作,并且想要针对特定XML进行量身定制的解析,因此您可以执行以下操作来摆脱(至少)不可读的代码。现在,谈论O(1)空间复杂度。假设XML很小,您可以一口气完整地阅读它。然后中断输入,似乎没有办法保持恒定的空间。
String[] persons = data.split("person[\\d]");
HashMap<Integer, StringBuilder> outputMap = new HashMap<>();
outputMap.put(0,new StringBuilder("Name | Age | Designation | Date | HomeTown"));
for(int i=0;i<persons.length;i++) {
if(persons[i].contains("<Name>")){
outputMap.put(i + 1, new StringBuilder()
.append(persons[i].split("</*Name>")[1]).append("|")
.append(persons[i].split("</*Age>")[1]).append("|")
.append(persons[i].split("</*Designation>")[1]).append("|")
.append(persons[i].split("</*Date>")[1]).append("|")
.append(persons[i].split("</*Hometown>")[1]));
}
}
Collection<StringBuilder> outputData = outputMap.values();
for(StringBuilder output: outputData){
System.out.println(output);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句