[英]Reading multiple lines of input from user using Buffer Reader
如何使用缓冲读取器获取两行输入,这些输入数据应以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>
您可以执行以下操作来实现您的目标。 我只发布你说你有困惑的循环部分。 您可以通过在容器中的每个项目和各个容器之间的空格之间放置:来要求用户输入容器项目。
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.