解析 截取xml文件分割成n个文件

public static void main(String[] args) {
    try {
        int batchSize = 1000; //每个文件包含的元素数量
        File inputFile = new File("D:\\demo.xml"); //输入文件
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(inputFile);
        doc.getDocumentElement().normalize();
        NodeList nodeList = doc.getElementsByTagName("Report_Entry"); //指定要分割的元素名称

        int totalElements = nodeList.getLength();
        int currentFileIndex = 1;

        for (int i = 0; i < totalElements; i += batchSize) {
            int endIndex = Math.min(i + batchSize, totalElements);
            String fileName = "output_" + currentFileIndex + ".xml"; //每个输出文件的名称
            FileWriter fileWriter = new FileWriter(fileName);
            fileWriter.write("\n");

            for (int j = i; j < endIndex; j++) {
                Element element = (Element) nodeList.item(j);
                fileWriter.write(getElementXML(element));
            }

            fileWriter.close();
            currentFileIndex++;
            break;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private static String getElementXML(Element element) {
    String xml = "";
    try {
        xml += "\n";
        xml += "" + element.getElementsByTagName("demo:WorkerID").item(0).getTextContent()+ "\n";
        xml += "" + element.getElementsByTagName("demo:Full_Name").item(0).getTextContent() + "\n";
        if (null != element.getElementsByTagName("demo:BUName") && null != element.getElementsByTagName("demo:BUName").item(0)){
            xml += "" + element.getElementsByTagName("demo:BUName").item(0).getTextContent() + "\n";
        }
        





        xml += "" + element.getElementsByTagName("demo:OrgName").item(0).getTextContent() + "\n";
        xml += "" + element.getElementsByTagName("demo:OrgName").item(0).getTextContent() + "\n";
        xml += "" + element.getElementsByTagName("demo:OrgID").item(0).getTextContent() + "\n";

        xml += "";
        xml += "" +element.getElementsByTagName("demo:Country").item(0).getChildNodes().item(1).getTextContent() + "";
        xml += "" + element.getElementsByTagName("demo:Country").item(0).getChildNodes().item(1).getTextContent() + ">";
        xml += "" + element.getElementsByTagName("wd:Country").item(0).getChildNodes().item(2).getTextContent() + ">";
        xml += "" + element.getElementsByTagName("demo:Country").item(0).getChildNodes().item(3).getTextContent() + ">";
        xml += "";
        
        xml += "" + element.getElementsByTagName("demo:CurrentCCCode").item(0).getTextContent() + "\n";
        xml += "" + element.getElementsByTagName("demo:CurrentCCName").item(0).getTextContent() + "\n";
        xml += "" + element.getElementsByTagName("demo:IsRehire").item(0).getTextContent() + "\n";
        xml += "" + element.getElementsByTagName("demo:HireDate").item(0).getTextContent() + "\n";
        if (null != element.getElementsByTagName("wd:CCChangeDate") && null != element.getElementsByTagName("demo:CCChangeDate").item(0)){
            xml += "" + element.getElementsByTagName("demo:CCChangeDate").item(0).getTextContent() + "\n";
        }
        if (null != element.getElementsByTagName("demo:ProposeCCCode") && null != element.getElementsByTagName("demo:ProposeCCCode").item(0)){
            xml += "" + element.getElementsByTagName(demo:ProposeCCCode").item(0).getTextContent() + "\n";
        }
        if (null != element.getElementsByTagName("demo:ProposeCCName") && null != element.getElementsByTagName("demo:ProposeCCName").item(0)){
            xml += "" + element.getElementsByTagName("demo:ProposeCCName").item(0).getTextContent() + "\n";
        }
        xml += "\n";
    } catch (Exception e) {
        e.printStackTrace();
    }
    return xml;
}

你可能感兴趣的:(xml,java,开发语言)