简体   繁体   中英

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:

<StoreList>
    <Store str_callingnumber="1234567890">
        <Number>Store1000</Number>
        <Street>123 USA BLVD</Street>
        <City>Nashville, TN 37211</City>
        <WUGID>1234</WUGID>
    </Store>
</StoreList>

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 javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.io.File;
import java.util.Scanner;

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

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

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

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

     // setting str_calling number attribute to store element

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

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

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

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

     Element wug = doc.createElement("WUGID");
     System.out.println("Enter the WUGID:");
     String wugid = sc.next();
     wug.appendChild(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) {
     e.printStackTrace();
  }
  }
  }

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

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 Node s 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 = sc.next();

//var number = sc.next();
//store.appendChild(number);

storenumber.setTextContent(number);
store.appendChild(storenumber);

Wash, rinse and repeat...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM