Ошибка синтаксического анализа в Java во время преобразования - PullRequest
0 голосов
/ 05 июня 2018

Я пытался преобразовать файл .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();
    }
}

, пожалуйста, помогите.Также мне нужно решение до сегодняшнего дня.заранее спасибо.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...