将根元素添加到XML

时间:2014-07-22 17:37:55

标签: java xml xmlroot

我是一个菜鸟,因为我之前没有任何java编程知识。

说,我已经设法将一些代码安排到一个工作的txt到xml转换器。

请特别注意以下注释:

我对代码的构建一无所知,将其视为从不同页面查找每个部分的人,并将其合并在一起,而不是一点点,但需要很多帮助。在报告此问题之前提醒一下。感谢

给出以下代码:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;

public class xml{

static class Bean {
    int id;
    String firstname;
    String lastname;
    String mail;

    public Bean(int id, String firstname, String lastname, String mail) {
        super();
        this.id = id;
        this.firstname = firstname;
        this.lastname = lastname;
        this.mail = mail;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getMail() {
        return mail;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }
}

private XStream xstream = new XStream();

public static void main(String[] args) throws IOException {

    new xml().process();
}

private void process() throws FileNotFoundException, IOException {

    xstream.alias("item", Bean.class);
    BufferedReader br = new BufferedReader(new FileReader("\\test.txt"));
    try {
        String line = br.readLine();
        line = br.readLine();
        while (line != null) {
            String[] split = line.split("\t");
            Bean bean = new Bean(new Integer(split[0]), split[1], split[2], split[3]);
            createBeanFile(bean);
            line = br.readLine();
        }
    } finally {
        br.close();
    }

}   

private void createBeanFile(Bean bean) throws IOException {

    BufferedWriter bw = new BufferedWriter
            (new OutputStreamWriter(new FileOutputStream("\\test.xml"),"UTF-8"));
    String str = xstream.toXML(bean);
    bw.write(str);
    bw.close();
    }
}

我如何以及在何处添加根元素以修改此当前输出:

<item>
  <id>56885</id>
  <firstname>LYTF</firstname>
  <lastname>LPRT</lastname>
  <mail>LYTF_LPRT@DERP.COM</mail>
</item>

对此:

<root>
    <item>
      <id>56885</id>
      <firstname>LYTF</firstname>
      <lastname>LPRT</lastname>
      <mail>LYTF_LPRT@DERP.COM</mail>
    </item>
</root>

1 个答案:

答案 0 :(得分:0)

我建议您研究XStreams library works如何复制现有的(执行不当的代码),并尝试根据您的需要对其进行修改。

您可以做的是添加一个根类,该类包含您的项目集合。

static class Root{

    List<Item> item = new ArrayList<Item>();

    public List<Item> getitem() {
        return item;
    }

    public void setBeans(List<Item> item) {
        this.item = item;
    }
}

在您的流程方法中,然后将以下属性设置为xStream。

xstream.alias("root", Root.class);
xstream.alias("item", Item.class); //I renamed the bean class to item here.
xstream.addImplicitCollection(Root.class, "item");

然后在你的while循环中,你会将这些项添加到根类并在循环之后写出XML。

while (line != null) {
            String[] split = line.split(",");
            Item item = new Item(new Integer(split[0]),split[1]);
            root.getitem().add(item);
            line = br.readLine();
        }

        createBeanFile(root);

希望这会让你开始。