简体   繁体   English

从Java中的文本文件生成XML

[英]Generate XML from a text file in Java

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: 我想使用XStream生成XML文件:

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? 如何生成xml文件? I need it. 我需要它。

I don't how you want your xml, but the following code : 我不是您想要XML的方式,而是以下代码:

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM