【问题标题】:We have to read an xml file and convert it into csv file and then store it in the path provided by the user我们必须读取一个 xml 文件并将其转换为 csv 文件,然后将其存储在用户提供的路径中
【发布时间】:2021-03-17 06:48:04
【问题描述】:

编写一个Java程序,用户需要提供文件系统(文件夹)的路径,然后他需要提供文件名。如果用户提供的文件名不是“order.xml”,它应该抛出异常。程序将读取用户提供的文件,并创建一个 csv 文件并将其存储在文件名为 order_output.csv 的同一路径中。

在C盘创建文件夹:

在上面的文件夹中创建一个文件:order.xml

应该使用的示例 XML 文件:

在 order_output.csv 中,您应该具有来自 XML 的以下属性

· Column1: OrderNo

· Column2:OrderDate(此字段应添加到 CSV 中,格式为 DD-MM-YYYY)

· Column3:PrimeLineNo

· Column4:SwatchFulfillmentType

· Column5:ExtnSwatchItemID

· Column6:名字

· Column7:ExtnAddressLine1

· Column8:邮政编码

【问题讨论】:

标签: java xml


【解决方案1】:
  1. 选择是使用 CLI 还是 GUI

那么让我们开始吧: 要检查文件是否为 .xml,您可以检查文件名的最后四个字符(最后四个是扩展名(“.xml”))。

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

现在用 if 语句检查它

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

现在您应该使用 XML 解析器(如 JDOM)读取文件。 之后,您必须将内容写入文本文件(CSV,您肯定知道,它只是一个文本文件,其单词以逗号或分号分隔)

【讨论】:

    猜你喜欢
    • 2018-07-07
    • 1970-01-01
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 2012-07-02
    • 2014-09-16
    相关资源
    最近更新 更多