Create XML file based on user input

Extreme Java newb...I am trying to create a program that will take user input and build an XML file based on input from the user. This file is used by a contact center platform to identify retail locations based on caller ID. The XML structure looks like this:

    <Store str_callingnumber="1234567890">
        <Street>123 USA BLVD</Street>
        <City>Nashville, TN 37211</City>

I want to prompt the user to enter the phone number, store number, street, city, and WUGID, but keep getting an error about cannot convert String to Node. All of the other information I see on here is about parsing an existing XML file or creating one with DOM...but I haven't found any hints on creating one based on user input. Here is my code thus far...

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.util.Scanner;

public class CreateXmlFileDemo {
public static void main(String argv[]) {
   Scanner sc = new Scanner(;

  try {
     DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
     DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
     Document doc = docBuilder.newDocument();

     // root element
     Element rootElement = doc.createElement("StoreList");

     // store element
     Element store = doc.createElement("Store");

     // setting str_calling number attribute to store element

     System.out.println("Enter the store phone number:");
     String phone =;
     Attr attr = doc.createAttribute("str_callingnumber");

     // store number element
     Element storenumber = doc.createElement("Number");
     System.out.println("Enter the store number:");
     var number =;

     //street address element
     Element street = doc.createElement("Street");
     System.out.println("Enter the street address:");
     String address =;

     Element zip = doc.createElement("City");
     System.out.println("Enter the city, state, and ZIP code:");
     String city =;

     Element wug = doc.createElement("WUGID");
     System.out.println("Enter the WUGID:");
     String wugid =;

     // write the content into xml file
     TransformerFactory transformerFactory = TransformerFactory.newInstance();
     Transformer transformer = transformerFactory.newTransformer();
     DOMSource source = new DOMSource(doc);
     StreamResult result = new StreamResult(new File("C:\\cars.xml"));
     transformer.transform(source, result);

     // Output to console for testing
     StreamResult consoleResult = new StreamResult(System.out);
     transformer.transform(source, consoleResult);
  } catch (Exception e) {

Thank you anyone/everyone for taking the time to help me understand.

1 answer

  • answered 2018-03-13 21:52 MadProgrammer

    You seemed to have the right idea to start with, but got off track somewhere.

    Basically, you're attempting to append a String value to a parent Node, but Node only accepts other Nodes as it children.

    Instead, you need to apply the String value to the textContent of the Element you've already created and append the Element to the parent Node

    Element storenumber = doc.createElement("Number");
    System.out.println("Enter the store number:");
    String number =;
    //var number =;

    Wash, rinse and repeat...