Java 怎么从文件创建XML STAX读卡器

2019年7月21日19:52:51Java 怎么从文件创建XML STAX读卡器已关闭评论 367 1028字阅读3分25秒

1.简介

Java 怎么从文件创建XML STAX读卡器?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */


import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import java.io.*;

public class Main {
    public static void main(String[] argv) throws Exception {
        String filename = "tah1986.com";
        System.out.println(createXmlReaderFromFile(filename));
    }

    
    static XMLEventReader createXmlReaderFromFile(String filename)
            throws IOException, XMLStreamException {
        String data = getXmlDataFromFile(filename);
        return createXmlReader(data);
    }

    private static String getXmlDataFromFile(String filename)
            throws IOException {
        try (BufferedReader bufferedReader = new BufferedReader(
                new FileReader(new File(filename)))) {
            String line;
            StringBuilder xmlData = new StringBuilder();

            while ((line = bufferedReader.readLine()) != null) {
                xmlData.append(line);
            }

            return xmlData.toString();
        }
    }

    
    static XMLEventReader createXmlReader(String xmlData)
            throws XMLStreamException {
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        return inputFactory.createXMLEventReader(new StringReader(xmlData));
    }
}

 
继续阅读
编程十万个怎么办