Home Is there an equivalent to xml or json that works natively in java
Reply: 1

Is there an equivalent to xml or json that works natively in java

luctowers
1#
luctowers Published in 2018-01-13 04:11:18Z

I'm working on a strictly java project for a computer science course. We're also not allowed to use any additional libraries. Are there any equivalents to xml or json to organize a small amount of data that can be parsed natively in java. Also if there is a way to natively parse either of those two that would be even better.

ArifMustafa
2#
ArifMustafa Reply to 2018-01-13 04:36:20Z

Use CSV file format.

CSV (Comma Separated Values) is a file format for data storage which looks like a text file. The information is organised with one record on each line and each field is separated by comma.

CSV File Characteristics

  1. One line for each record.
  2. Comma separated fields.
  3. Space-characters adjacent to commas are ignored.
  4. Fields with in-built commas are separated by double quote characters.
  5. Fields with double quote characters must be surrounded by double quotes. Each inbuilt double quote must be represented by a pair of consecutive quotes
  6. Fields that contain inbuilt line-breaks must be surrounded by double quotes

CSV Advantages

  1. CSV is human readable and easy to edit manually

  2. CSV is simple to implement and parse

  3. CSV is processed by almost all existing applications

  4. CSV provides a straightforward information schema

  5. CSV is faster to handle

  6. CSV is smaller in size

  7. CSV is considered to be standard format

  8. CSV is compact. For XML you start tag and end tag for each column in each row. In CSV you write the column headers only once.

  9. CSV is easy to generate.

Example is as follows:

employees.csv

1,Pankaj Kumar,Developer,5000 USD
2,Mani,Programmer,4000 USD
3,Avinash,Developer,5000 USD
4,David,QA Lead,4000 USD

Employees class:

public class Employee {

    private int id;
    private String name;
    private String role;
    private String salary;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getRole() {
        return role;
    }
    public void setRole(String role) {
        this.role = role;
    }
    public String getSalary() {
        return salary;
    }
    public void setSalary(String salary) {
        this.salary = salary;
    }

    @Override
    public String toString(){
        return "\nID="+getId()+"::Name"+getName()+"::Role="+getRole()+"::Salary="+getSalary();
    }
}

ReadCSVWithScanner class:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ReadCSVWithScanner {

    public static void main(String[] args) throws IOException {
        // open file input stream
        BufferedReader reader = new BufferedReader(new FileReader(
                "employees.csv"));

        // read file line by line
        String line = null;
        Scanner scanner = null;
        int index = 0;
        List<Employee> empList = new ArrayList<>();

        while ((line = reader.readLine()) != null) {
            Employee emp = new Employee();
            scanner = new Scanner(line);
            scanner.useDelimiter(",");
            while (scanner.hasNext()) {
                String data = scanner.next();
                if (index == 0)
                    emp.setId(Integer.parseInt(data));
                else if (index == 1)
                    emp.setName(data);
                else if (index == 2)
                    emp.setRole(data);
                else if (index == 3)
                    emp.setSalary(data);
                else
                    System.out.println("invalid data::" + data);
                index++;
            }
            index = 0;
            empList.add(emp);
        }

        //close reader
        reader.close();

        System.out.println(empList);        
    }

}

Once we run above program, it prints following output:

[ID=1::NamePankaj Kumar::Role=Developer::Salary=5000 USD, 
ID=2::NameMani::Role=Programmer::Salary=4000 USD, 
ID=3::NameAvinash::Role=Developer::Salary=5000 USD, 
ID=4::NameDavid::Role=QA Lead::Salary=4000 USD]
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.304574 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO