简体   繁体   English

我们必须读取一个 xml 文件并将其转换为 csv 文件,然后将其存储在用户提供的路径中

[英]We have to read an xml file and convert it into csv file and then store it in the path provided by the user

Write a Java program where user needs to provide the path to a file system (Folder) and then he needs to provide filename.编写一个 Java 程序,用户需要提供文件系统(文件夹)的路径,然后他需要提供文件名。 If user provides file name other than “order.xml”, it should throw exception.如果用户提供的文件名不是“order.xml”,它应该抛出异常。 Program will read the file provided by user and it'll create a csv file and store it on the same path with filename as order_output.csv.程序将读取用户提供的文件,并创建一个 csv 文件并将其存储在与 order_output.csv 文件名相同的路径中。

Create a folder in C drive:在 C 驱动器中创建一个文件夹:

Create a file: order.xml in the above folder在上面的文件夹中创建一个文件:order.xml

Sample XML file that should be used:应使用的示例 XML 文件:

In the order_output.csv, you should have following attributes from the XML在 order_output.csv 中,您应该具有来自 XML 的以下属性

· Column1: OrderNo · Column1: OrderNo

· Column2: OrderDate (this field should be added in the CSV as format DD-MM-YYYY) · Column2:OrderDate(此字段应在CSV中添加为DD-MM-YYYY格式)

· Column3: PrimeLineNo · Column3:PrimeLineNo

· Column4: SwatchFulfillmentType · Column4:SwatchFulfillmentType

· Column5: ExtnSwatchItemID · Column5:ExtnSwatchItemID

· Column6: FirstName · Column6:名字

· Column7: ExtnAddressLine1 · Column7:ExtnAddressLine1

· Column8: ZipCode · Column8:邮政编码

  1. Choose if you wanna use CLI or GUI选择是否要使用 CLI 或 GUI

Then let's begin: To check if the file is a.xml you could check the last four char of the file name (the last four are the extension (".xml")).然后让我们开始:要检查文件是否为 a.xml,您可以检查文件名的最后四个字符(最后四个是扩展名(“.xml”))。

   String#substring(string.length-4) //Get the substring of the given one from last four chars

Now check it with an if statement现在用 if 语句检查它

  if(substring == ".xml"){
        //stuff here
  } 

Now you should read the file with a XML Parser (as JDOM ).现在您应该使用 XML 解析器(作为JDOM )读取文件。 After that you have to write the content on the text file (CSV, as you sure know, it's just a text file whose words are separated by comma or semicolon)之后,您必须将内容写入文本文件(CSV,您肯定知道,它只是一个文本文件,其单词以逗号或分号分隔)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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