Generate XML from a text file in Java

狂风中的少年 提交于 2019-12-25 03:30:05

问题


This is my text file:

5625145214 6
8562320154 2
8542154157 5
6325145214 5
5214214584 6
5625142224 3
8562456754 1

I want to use XStream to generate XML file:

This is my code:

    private static void generateXml() throws IOException {
    XStream xStream = new XStream(new DomDriver());

    String line = null;
    try (BufferedReader br = new BufferedReader(new FileReader("Unique Numbers.txt"))) {
        while ((line = br.readLine()) != null) {
            String xml = xStream.toXML(line);
            System.out.println(xml);
        }
    }

}

How can i generate xml file? I need it.


回答1:


I don't how you want your xml, but the following code :

public static void main(String[] args) {        
        generateXml();
    }
     private static void generateXml()  {
            XStream xStream = new XStream(new DomDriver());

            String line = null;
            try{
                BufferedReader br = new BufferedReader(new FileReader(new File("Unique Numbers.txt"))) ;

                while ((line = br.readLine()) != null) {
                    String xml = xStream.toXML(line);
                    System.out.println(xml);
                }
            }catch(IOException ioe){
                System.out.println(ioe.getMessage());
            }

            }

would print :

<string>5625145214 6</string>
<string>8562320154 2</string>
<string>8542154157 5</string>
<string>6325145214 5</string>
<string>5214214584 6</string>
<string>5625142224 3</string>
<string>8562456754 1</string>


来源:https://stackoverflow.com/questions/25230086/generate-xml-from-a-text-file-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!