Search code examples
javaxmlxmlroot

Add a root element to a XML


I'm quite a noob, since I don't have any previous knowledge of java programming.

Said that, I've managed to arrange some code into a working txt to xml converter.

Please take extreme consideration of the following note:

I have no clue at all about the building of the code, see it as someone who look for each piece from a different page and merge it together with, not a little, but a lot of help. Remind of that prior to report this question. Thanks

Given the following code:

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();
    }
}

How and where can I add a root element in order to modify my current ouput from this:

<item>
  <id>56885</id>
  <firstname>LYTF</firstname>
  <lastname>LPRT</lastname>
  <mail>[email protected]</mail>
</item>

To this:

<root>
    <item>
      <id>56885</id>
      <firstname>LYTF</firstname>
      <lastname>LPRT</lastname>
      <mail>[email protected]</mail>
    </item>
</root>

Solution

  • I would urge you to study how the XStreams library works instead of copying existing (badly implemented code) and try to modify it to your needs.

    What you could do is to add a root class, which holds collection of your items.

    static class Root{
    
        List<Item> item = new ArrayList<Item>();
    
        public List<Item> getitem() {
            return item;
        }
    
        public void setBeans(List<Item> item) {
            this.item = item;
        }
    }
    

    in your process method, you then set the following properties to xStream.

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

    and in your while loop, you would then add the items to the root class and write out the XML after the loop.

    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);
    

    Hopefully this will get your started.