简体   繁体   中英

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. If user provides file name other than “order.xml”, it should throw exception. 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.

Create a folder in C drive:

Create a file: order.xml in the above folder

Sample XML file that should be used:

In the order_output.csv, you should have following attributes from the XML

· Column1: OrderNo

· Column2: OrderDate (this field should be added in the CSV as format DD-MM-YYYY)

· Column3: PrimeLineNo

· Column4: SwatchFulfillmentType

· Column5: ExtnSwatchItemID

· Column6: FirstName

· Column7: ExtnAddressLine1

· Column8: ZipCode

  1. Choose if you wanna use CLI or 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")).

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

Now check it with an if statement

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

Now you should read the file with a XML Parser (as 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)

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