0

I have created an xml file and I want to save it to my desktop but I have no clue how to do that type of thing.

here is my code thus far:

// create xml
        DocumentBuilderFactory factory =
            DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();

        Document testDoc = builder.newDocument();

        Element bo = testDoc.createElement("bo");
        bo.setAttribute("type", "Employee");
        bo.setAttribute("id", emp.getId());
        testDoc.appendChild(bo);

        Element username = testDoc.createElement("username");
        username.setTextContent(emp.getUsername());
        bo.appendChild(username);

        Element passHash = testDoc.createElement("passwordHash");
        passHash.setTextContent(emp.getPasswordHash());
        bo.appendChild(passHash);

        Element passwordSalt = testDoc.createElement("passwordSalt");
        passwordSalt.setTextContent(emp.getPasswordSalt());
        bo.appendChild(passwordSalt);

        Element name = testDoc.createElement("name");
        name.setTextContent(emp.getName());
        bo.appendChild(name);

        Element lastLogin = testDoc.createElement("lastLogin");


        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("date = " + emp.getLastLogin());
        String date = df.format(emp.getLastLogin());
        lastLogin.setTextContent(date);
        bo.appendChild(lastLogin);

        DOMSource source = new DOMSource(testDoc);

        PrintStream ps = new PrintStream(emp.getId() + ".xml");
        StreamResult result = new StreamResult(ps);

        TransformerFactory transformerFactory = TransformerFactory
            .newInstance();
        Transformer transformer = transformerFactory.newTransformer();

        transformer.transform(source, result);

Thanks,

novicePrgrmr
  • 18,647
  • 31
  • 81
  • 103

1 Answers1

4

It looks like you've figured out to build and write XML to a file, and are really asking how to create the file on the user's desktop.

If so, read the accepted answer to In java under Windows, how do I find a redirected Desktop folder?

Incidentally, the solution is not Windows specific.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I know it seems weird but I am sincerely fried and really outside my programming comfort zone. thanks! – novicePrgrmr Apr 16 '11 at 02:27
  • +1, for interpreting the question (and giving a solution). All the XML code led me on a wild goose chase. I wonder why the answer hasn't been accepted yet? – camickr Apr 16 '11 at 02:57