Convert Zaurus Addressbook xml to csv (e.g. for Thunderbird)

Some years ago my father gave me a Zaurus – a PDA-like-device. But in the mean time the battery losts its energy and the only usecase for me was the addressbook. Although the Zaurus was a nice PDA, where you can programm Java GUI’s (via awt, e.g. here or here) and other nice things.

I used the csv file to import my address-data into Thunderbird: in the menu bar under Extras->Import you can choose the csv format for the addressbook and verify every entry in the csv. It will not overwrite anything, because it will create an entirely fresh addressbook named like the csv file, so feel free to try it out and develop it to your needs. If you have another usecase for this source please let me know in the comments.

But today here is the Java source code, so that you can convert the addressbook.xml to an csv and be independent from Zaurus (and its battery life):

package zaurus2thunderbird;

import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * @author Peter Karich, peat_hal 'at' users 'dot' sourceforge 'dot' net
 */
public class Main {

    public static void main(String[] args) throws Exception {
        new Main().start();
    }

    public void start() throws Exception {
        // Create a factory
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        // Use the factory to create a builder
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse("addressbook.xml");

        // Get a list of all contacts in the document
        NodeList list = doc.getElementsByTagName("Contact");

        // maps oldKeys to newKeys, but store order of how elements are added
        Map<String, String> map = new LinkedHashMap<String, String>() {

            {
                put("FirstName", "Vorname");
                put("LastName", "Nachname");
                put("MiddleName", "Spitzname");

                put("BusinessStreet", "Dienstlich: Adresse");
                put("BusinessState", "Dienstlich: Adresse 2");
                put("BusinessCity", "Dienstlich: Stadt");
                put("BusinessPhone", "Tel. dienstlich");
                put("BusinessFax", "Fax-Nummer");
                put("BusinessMobile", "Mobil-Tel.-Nr.");

                put("HomeStreet", "Privat: Adresse");
                put("HomeState", "Privat: Adresse 2");
                put("HomeCity", "Privat: Stadt");
                put("HomePhone", "Tel. privat");
                put("HomeFax", "Fax-Nummer");
                put("HomeMobile", "Mobil-Tel.-Nr.");

                put("JobTitle", "Arbeitstitel");
                put("Company", "Organisation");
                put("Department", "Abteilung");

                put("Anniversary", "Geburtstag");
                put("Birthday", "Geburtstag");

                put("DefaultEmail", "Primäre E-Mail");
                put("Emails", "Sekundäre E-Mail");

                put("Categories", "Notizen");

                put("FileAs", "");
                put("rid", "");
                put("Gender", "");
                put("Uid", "");
                put("rinfo", "");
            }
        };

        // maps newKeys to values
        LinkedHashMap<String, String> oneLine = new LinkedHashMap<String, String>();
        for (String newKey : map.values()) {
            oneLine.put(newKey, "");
        }

        // print csv header
        printLine(map.values());

        Set<String> missing = new HashSet<String>();
        for (int i = 0; i < list.getLength(); i++) {
            Map<String, String> cloneForNewLine = (Map<String, String>) oneLine.clone();
            NamedNodeMap attr = list.item(i).getAttributes();
            for (Entry<String, String> entry : map.entrySet()) {
                Node newAttr = attr.getNamedItem(entry.getKey());
                if (newAttr != null)
                    cloneForNewLine.put(entry.getValue(), newAttr.getTextContent());
            }

            // print csv values
            printLine(cloneForNewLine.values());

            // add items if an old attribute does not exist in map
            for (int j = 0; j < attr.getLength(); j++) {
                String newAttr = map.get(attr.item(j).getNodeName());
                if (newAttr == null)
                    missing.add(attr.item(j).getNodeName());
            }
        }

        System.out.println("\nMissing new mappings for old attributes:");
        printLine(missing);
    }

    public void printLine(Collection<String> items) {

        for (String m : items) {
            System.out.print(m + ",");
        }
        System.out.println();
    }
}

4 thoughts on “Convert Zaurus Addressbook xml to csv (e.g. for Thunderbird)

  1. Hello,

    I’d be interested in the source code, because my Zaurus also seems to have reache the end of its lifetime.

    Would you please send it to me?

    Thanks a lot!

Comments are closed.