在转换过程中解析Java中的错误

时间:2018-06-05 06:04:57

标签: java xml parsing

我试图将.txt文件转换为.xml,我遇到了以下问题。 。如何拆分字符     <BookSet> <Authorname>10_1 J Martin; James L Crowley</Authorname> 就......而言 ;和号码后的空格。

到目前为止,这是我的代码。

public class CONVTOXML2 {

    BufferedReader in;
    StreamResult out;
    TransformerHandler th;
    AttributesImpl atts;

    public static void main(String args[]) {
        new CONVTOXML2().doit();
    }

    public void doit() {
        try {
            in = new BufferedReader(new FileReader("E:/Java Codes/JMartin.txt"));
            out = new StreamResult("E:/Java Codes/JMartin69.xml");
            initXML();
            String str;
            while ((str = in.readLine()) != null) {
                  process(str);
            }
            in.close();
            closeXML();
        } catch (IOException | ParserConfigurationException | TransformerConfigurationException | SAXException e) {
        }
    }

    public void initXML() throws ParserConfigurationException,
            TransformerConfigurationException, SAXException {
        SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory
                .newInstance();

        th = tf.newTransformerHandler();
        Transformer serializer = th.getTransformer();
        serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
        serializer.setOutputProperty(
                "{http://xml.apache.org/xslt}indent-amount", "4");
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        th.setResult(out);
        th.startDocument();
        atts = new AttributesImpl();
        th.startElement("", "", "Author", atts);
    }

    public void process(String s) throws SAXException {
        String[] elements = s.split("<>;");
        atts.clear();
        th.startElement("", "", "Data", atts);
        th.startElement("", "/t", "AuthorID", atts);
                th.startElement("/t","<", "AuthorName",atts);
                th.startElement("<",">", "Title", atts);
                th.startElement(">","", "Venue", atts);
        th.characters(elements[1].toCharArray(), 0, elements[0].length());
                th.endElement(">","", "Venue");
                th.endElement("/t","<", "AuthorName");
                th.endElement("<", ">","Title");
        th.endElement("", "/t", "AuthorID");
        th.endElement("", "", "Data");
    }

    public void closeXML() throws SAXException {
        th.endElement("", "", "Author");
        th.endDocument();
    }
}

请帮忙。此外,我今天需要解决方案。提前谢谢。

0 个答案:

没有答案