Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Sax解析器-无法将XML文件拆分为指定大小_Java_Xml_Parsing_Sax_Saxparser - Fatal编程技术网

Java Sax解析器-无法将XML文件拆分为指定大小

Java Sax解析器-无法将XML文件拆分为指定大小,java,xml,parsing,sax,saxparser,Java,Xml,Parsing,Sax,Saxparser,如何读取xml并使用SAX解析器将其拆分为多个文件,我遇到了一些困难。考虑到我们输入了以下生成的XML: <?xml version="1.0" encoding="utf-8"?> <record-table> <record> <record_id>12345</record_id> <record_rows> <record_row>str1234</record_ro

如何读取xml并使用SAX解析器将其拆分为多个文件,我遇到了一些困难。考虑到我们输入了以下生成的XML:

<?xml version="1.0" encoding="utf-8"?>
<record-table>
  <record>
    <record_id>12345</record_id>
    <record_rows>
      <record_row>str1234</record_row>
    </record_rows>
  </record>
  <footer>
    <record_count>12345</record_count>
    <record_row_count>12345</record_row_count>
  </footer>
</record-table>

此时此刻,我多次尝试读取它,程序执行,但没有执行任何操作

守则草案:

    public static void splitXML(File fileToSplit, int splitFileSize) {
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();
        XMLReader reader = parser.getXMLReader();

        reader.parse(new InputSource(new FileInputStream(fileToSplit)));
        reader.setContentHandler(new DefaultHandler() {

            public static final String DIRECTORY = "target/results";

            private int fileSize = 0;

            private File fileLocation;

            // counts number of files created
            private int fileCount = 0;

            // counts characters to decide where to split file
            private long charCount = 0;
            // data line buffer (is reset when the file is split)
            private StringBuilder recordRowDataLines = new StringBuilder();

            // temporary variables used for the parser events
            private String currentElement = null;
            private String currentRecordId = null;
            private String currentRecordRowData = null;

            public final long TAG_CHAR_SIZE = 5;

            @Override
            public void startDocument() throws SAXException {
                File directory = new File(DIRECTORY);
                if(!directory.exists())
                    directory.mkdir();
            }

            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                currentElement = qName;
            }

            @Override
            public void endElement(String uri, String localName, String qName) throws SAXException {
                if(qName.equals("record_rows")) {
                    try {
                        savePatch();
                    } catch (IOException e) {
                        throw new SAXException(e);
                    }
                }
                if (qName.equals("record_row")) { // one record finished - save in buffer & calculate size so far
                    charCount += tagSize("record_row");
                    recordRowDataLines.append("<record_row>")
                            .append(currentRecordRowData)
                            .append("</record_row>");
                    if (charCount >= fileSize) { // if max size was reached, save what was read so far in a new file
                        try {
                            savePatch();
                        } catch (IOException ex) {
                            throw new SAXException(ex);
                        }
                    }
                }
                currentElement = null;
            }

            @Override
            public void characters(char[] ch, int start, int length) throws SAXException {
                System.out.println(new String(ch, start, length));
                if (currentElement == null) {
                    return;
                }
                if (currentElement.equals("record_id")) {
                    currentRecordId = new String(ch, start, length);
                }
                if (currentElement.equals("record_row")) {
                    currentRecordRowData = new String(ch, start, length);
                    charCount += currentRecordRowData.length(); // storing size so far
                }
            }

            public long tagSize(String tagName) {
                return TAG_CHAR_SIZE + tagName.length() * 2; // size of text + tags
            }

            public void savePatch() throws IOException {
                ++fileCount;
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("<record part='")
                        .append(fileCount)
                        .append("'><record_id>")
                        .append(currentRecordId)
                        .append("</record_id>")
                        .append("<record_rows>")
                        .append(recordRowDataLines)
                        .append("</record_rows></record>");
                File fragment = new File(DIRECTORY, "data_part_" + fileCount + ".xml");
                System.out.println("File " + fragment.getAbsolutePath() + "has been saved!");

                try(FileWriter out = new FileWriter(fragment)){
                    out.write(stringBuilder.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }

                //flush current information that was saved.
                recordRowDataLines = new StringBuilder();
                charCount = 0;
            }
        });

    } catch (ParserConfigurationException | SAXException | IOException e) {
        e.printStackTrace();
    }
}
主类外观:

public class Main {

    public static void main(String[] args) {
        System.out.println("Welcome!");

        <omitted>
        File f = CommonUtils.requestFilePath();
        int fileSize = CommonUtils.requestUserValueInt("Enter file split size : ");
        XMLSplitter.splitXML(f, fileSize);
    }
}

请你看看,我看不到的。请提供帮助。

您应该在解析之前调用setContentHandler。

程序会执行,但不会执行任何操作,因为范围太广。你调试代码了吗?你应该缩小范围,给我们一个更具体的问题描述。您不应该将XML创建为普通字符串。你有没有想过逃离特殊角色?改用。是的,我做了调试,一切都很好。这就是我喝咖啡的原因。现在,我不知道如何将footer元素与实际元素计数一起附加到每个分割的文件中……程序执行了两条语句,但没有执行任何操作,是的,我进行了调试,一切都很好。是矛盾的。SAX是必需的吗?在我看来,如果您不局限于SAX,那么这样做可能会更快/更简单
public class Main {

    public static void main(String[] args) {
        System.out.println("Welcome!");

        <omitted>
        File f = CommonUtils.requestFilePath();
        int fileSize = CommonUtils.requestUserValueInt("Enter file split size : ");
        XMLSplitter.splitXML(f, fileSize);
    }
}