如何在不使用 DOM 或 Java 中的 SAX 解析器的情况下解析 XML?

How to Parse XML without using DOM or SAX Parser in Java?

我试图解析一个 XML 文件并将数据写入 CSV 文件而不使用 DOM 或仅通过普通 Java.

的 SAX 解析器

输出应采用以下格式

姓名 |年龄 |名称 |日期 |故乡

史蒂夫,21 岁,职员,2013 年 7 月 14 日,加利福尼亚州

约翰,24 岁,职员,2014 年 10 月 17 日,纽约

菲尔,45 岁,校长,2015 年 8 月 20 日,库比蒂诺

我当前的输出格式

学生总数=3

Parent 数据:student1

姓名=米克

年龄=21

指定=学生

日期=2015 年 7 月 10 日

家乡=金奈

Parent 数据:student2

姓名=史蒂夫

年龄=22

职称=员工

日期=2015 年 7 月 10 日

家乡=库比蒂诺

Parent 数据:student3

姓名=史蒂夫

年龄=22

职称=员工

日期=2015 年 7 月 15 日

家乡=库比蒂诺

Parent 数据:student4

姓名=史蒂夫

年龄=22

职称=职员

日期=2015 年 7 月 18 日

家乡=库比蒂诺

当读取 XML 检查标签中的员工总数时,如果总数为 3,并且 xml 标签总数为三个(即员工详细信息标签总数),则三个员工详细信息将以上述指定格式写入 CSV 文件和 CSV(日期和时间明智的文件名)文件,否则需要创建错误日志。

我无法打印这种格式的输出。请帮我解决这个问题,还有一件事代码应该是 O(n) 时间复杂度和 O(1) space 复杂度。如果您粘贴完整的解决方案而不是方法,那将对我有所帮助,

提前致谢。

这是我的代码

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) space 复杂性。假设 XML 很小,您可以一次性完整地阅读它;然后打破输入,似乎没有办法有常量 space.

    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);
    }