使用缓冲区读取器从用户读取多行输入

时间:2019-06-17 19:22:48

标签: java xml

如何使用缓冲读取器进行两行输入,这些输入数据应显示在XML元素中?每行输入用“ :”分隔

示例:

id:streetname:city:state:zipcode:country
121:xxxx:slm:tn:636007:ind
123:yyyy:chn:tn:600009:ind 

我可以使用

读取单行输入
String line = buf.readLine();
String[] columns = line.split(":");

,并且这些数据已成功以XML填充。
我需要两行输入以XML填充的帮助。

    public static void main(String args[]) throws IOException{
        DocumentBuilderFactory docFactory =  DocumentBuilderFactory.newInstance();
        docFactory.setNamespaceAware(true);
        DocumentBuilder docBuilder;
        try {
            docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.newDocument();
            Element rootElement = doc.createElementNS("www.DATA.com/XML", "Contact");
            doc.appendChild(rootElement);
            BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter details of shipments:");
            String line=buf.readLine();
            String[] columns = line.split(":");
            String id = columns[0];
            String street = columns[1];
            String city = columns[2];
            String state = columns[3];
            String zipcode = columns[4];
            String country = columns[5];

            // create a book object from the data in the columns
            trial p = new trial(id, street, city,state,zipcode,country);

            // close the input stream
            buf.close();
            //append first child element to root element
            rootElement.appendChild(getShipmentDetails(doc, id,street, city,state,zipcode,country));

            // 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("ContactDetails.xml"));
            transformer.transform(source, result);

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

    private static Node getShipmentDetails(Document doc, String id, String street, String city, String state, String zipcode, String country  ) 
    {
        Element address = doc.createElement("address");

        //set id attribute
        address.setAttribute("addressId", id);

        //create street element
        address.appendChild(getShipmentElements(doc, address, "street", street));

        //create city element
        address.appendChild(getShipmentElements(doc, address, "city", city));

        //create state element
        address.appendChild(getShipmentElements(doc, address, "state", state));

        //create zipcode element
        address.appendChild(getShipmentElements(doc, address, "zipcode", zipcode));

        //create country element
        address.appendChild(getShipmentElements(doc, address, "country", country));

        return address;
    }

//utility method to create text node

    private static Node getShipmentElements(Document doc, Element element, String name, String value) {
        Element node = doc.createElement(name);
        node.appendChild(doc.createTextNode(value));
        return node;
    }
}

预期输出:

<address id="121">
  <street>xxxx</street>
  <city>xxxx</city>
  <state>xxxx</state>
  <zipcode>xxxx</zipcode>
  <country>xxxx</country>
</address>
<address id="123">
  <street>xxxx</street>
  <city>xxxx</city>
  <state>xxxx</state>
  <zipcode>xxxx</zipcode>
  <country>xxxx</country>
</address>

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作来实现所需的功能。我只在循环部分发布您认为有混淆的地方。您可以通过在容器中的每个项目之间以及各个容器之间的空格之间放置一个:来要求用户输入容器项目。

BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter details of shipments with *ONLY ONE* space in between multiple lines. Please separate items in one shipping container with :");
        String line = buf.readLine();

        //First separate out the space.
        String[] columnStrings = line.split(" ");

        for (String columnString : columnStrings) {
            String[] columns = columnString.split(":");
            String id = columns[0];
            String street = columns[1];
            String city = columns[2];
            String state = columns[3];
            String zipcode = columns[4];
            String country = columns[5];
            // close the input stream
            buf.close();
            //append first child element to root element
            rootElement.appendChild(getShipmentDetails(doc, id, street, city, 
state, zipcode, country));
        }

此外,我不知道您为什么使用试用声明。我将其删除,程序编译正常。您可以遍历每行,并继续在XML中添加更多元素。上述代码执行后的最终XML是:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Contact xmlns="www.DATA.com/XML">
<address addressId="121">
    <street>xxxx</street>
    <city>slm</city>
    <state>tn</state>
    <zipcode>636007</zipcode>
    <country>ind</country>
</address>
<address addressId="123">
    <street>yyyy</street>
    <city>chn</city>
    <state>tn</state>
    <zipcode>600009</zipcode>
    <country>ind</country>
</address>

请尝试一下此代码,然后查看。谢谢

您可以一次从用户获取一行内容,而不必一次从用户那里获取全部输入内容。第一件事是询问用户您可以执行的操作数量

Scanner input = new Scanner(System.in);
System.out.println("Enter number of shipments");
String noOfShipments = input.nextLine();
//NumberUtils class is from apache commons
if(!NumberUtils.isNumber(noOfShipments)) {
    throw new IllegalArgumentException("The number of shipments is supposed to be an integer");
}
int shipmentNo = Integer.parseInt(noOfShipments);
for(int i=0;i<shipmentNo;i++) {
    System.out.println("Enter shipment details for shipment no "+(i+1));
    String shipmentDetail = input.nextLine();  // Read shipment details
    //Do something with this input and keep parsing until the end of the for loop
}
相关问题