Java对象数据的XML和JSON表示

南楼画角 提交于 2020-03-01 04:13:03

XML与JSON

这是实战Demo节目,自然不会是讲原理的,事先声明!

本文主要是基于之前的一份Java代码,根据合理数据(下面的链接中有),设计对应的XML格式和JSON格式的存储表示,并使用工具对其合法性加以验证。

JavaCode

先看Java的代码,完整版在这里 → @see 完整版

下面是这里会用到的的部分类:

/**
 * Product is created to be used as the super class.
 * @author BlankSpace
 * @version 2.0
 */
public class Product {
    private String code;
    private String description;
    private double price;

    public Product(String code, String description, double price) {
        this.code = code;
        this.description = description;
        this.price = price;
    }

    public String getCode() {
        return this.code;
    }

    public String getDescription() {
        return this.description;
    }

    public double getPrice() {
        return this.price;
    }

    //Identifying whether objects are equal by code.
    @Override
    public boolean equals(Object product) {
        return (product instanceof Product) && (this.getCode().equals(((Product)product).getCode()));
    }

    @Override
    public String toString() {
        return this.getCode() + "," + this.getDescription() + "," + this.getPrice();
    }

}
/**
 * An orderItem contains one certain type of product and the quantity of user needs.
 * The class's objects will be added into an order's item list.
 * @author BlankSpace
 * @version 2.0
 */
public class OrderItem {
    private Product product;
    private int     quantity;

    public OrderItem(Product product, int quantity) {
        this.product = product;
        this.quantity = quantity;
    }

    public Product getProduct() {
        return this.product;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public int getQuantity() {
        return this.quantity;
    }

    /**
     * to calculate the cost of purchasing the product
     * @return the result
     */
    public double getValue() {
        return this.getProduct().getPrice() * this.getQuantity();
    }

    @Override
    public String toString() {
        return this.getQuantity() + "," + this.getProduct().getCode() + "," + this.getProduct().getPrice();
    }

}
import java.util.ArrayList;

/**
 * the class which saves a certain order's all orderItems
 * @author BlankSpace
 * @version 2.0
 */
public class Order {
    //to save all orderItems.
    private ArrayList<OrderItem> orderItemList = new ArrayList<>();

    /**
     * to add the orderItem to the list.
     * @param orderItem
     */
    public void addOrderItem(OrderItem orderItem) {
        this.orderItemList.add(orderItem);
    }

    /**
     * to delete the orderItem which user wants to delete.
     * @param orderItem
     */
    public void removeOrderItem(OrderItem orderItem) {
        this.orderItemList.remove(orderItem);
    }

    /**
     * @return the list which saves all orderItems
     */
    public ArrayList<OrderItem> getAllOrderItem(){
        return this.orderItemList;
    }

    /**
     * to search the orderItem which user wants to find in the list.
     * @param product
     * @return the orderItem which user wants to find
     */
    public OrderItem getOrderItem(Product product) {
        for (OrderItem orderItem : orderItemList) {
            if (orderItem.getProduct().equals(product)) {
                return orderItem;
            }
        }
        return null;
    }

    /**    
     * @return the number of orderItems which are saved in the list.
     */     
    public int getNumberOfOrderItems() {
        return this.orderItemList.size();
    }

    /**
     * to sum all the orderItems' value in the list.
     * @return the sum of all the orderItems' value.
     */
    public double getValue() {
        double value = 0.0;
        for (OrderItem orderItem : orderItemList) {
            value += orderItem.getValue();
        }
        return value;
    }

}
import java.util.ArrayList;

/**
 * the class which can save all orders.
 * @author BlankSpace
 * @version 2.0
 */
public class Sales {
    //to save all orders.
    private ArrayList<Order> orders = new ArrayList<>();

    /**
     * to add the order to the list.
     * @param order
     */
    public void addOrder(Order order) {
        this.orders.add(order);
    }

    /**
     * @return the list which saves all orders
     */
    public ArrayList<Order> getAllOrder(){
        return this.orders;
    }
    
    /**
     * @return the number of orders which are saved in the list.
     */
    public int getNumberOfOrders() {
        return this.orders.size();
    }

}

使用XML格式存储

可以将Sales对象的数据使用XML格式来存储。

XML

<?xml version="1.0" encoding="UTF-8" ?>
<SALES>
    <ORDER>
        <ORDERITEM>
            <PRODUCT>
                <CODE>A001</CODE>
                <DESCRIPTION>Almond Flavored Syrup</DESCRIPTION>
                <PRICE>9.0</PRICE>
            </PRODUCT>
            <QUANTITY>3</QUANTITY>
        </ORDERITEM>
        <ORDERITEM>
            <PRODUCT>
                <CODE>A002</CODE>
                <DESCRIPTION>Irish Creme Flavored Syrup</DESCRIPTION>
                <PRICE>9.0</PRICE>
            </PRODUCT>
            <QUANTITY>5</QUANTITY>
        </ORDERITEM>
    </ORDER>
    <ORDER>
        <ORDERITEM>
            <PRODUCT>
                <CODE>A001</CODE>
                <DESCRIPTION>Almond Flavored Syrup</DESCRIPTION>
                <PRICE>9.0</PRICE>
            </PRODUCT>
            <QUANTITY>1</QUANTITY>
        </ORDERITEM>
        <ORDERITEM>
            <PRODUCT>
                <CODE>A004</CODE>
                <DESCRIPTION>Caramel Flavored Syrup</DESCRIPTION>
                <PRICE>9.0</PRICE>
            </PRODUCT>
            <QUANTITY>2</QUANTITY>
        </ORDERITEM>
        <ORDERITEM>
            <PRODUCT>
                <CODE>A006</CODE>
                <DESCRIPTION>Gourmet Coffee Travel Thermo</DESCRIPTION>
                <PRICE>18.0</PRICE>
            </PRODUCT>
            <QUANTITY>3</QUANTITY>
        </ORDERITEM>
    </ORDER>
    <ORDER>
        <ORDERITEM>
            <PRODUCT>
                <CODE>A009</CODE>
                <DESCRIPTION>Gourmet Coffee 36 Cup Filters</DESCRIPTION>
                <PRICE>45.0</PRICE>
            </PRODUCT>
            <QUANTITY>100</QUANTITY>
        </ORDERITEM>
    </ORDER>
</SALES>

浏览器解析XML

在这里插入图片描述

XML解析错误

如果我们删去最后一行的<\SALES>,就会解析错误:
在这里插入图片描述
在这里插入图片描述

使用JSON格式存储

同样的数据,也可以使用JSON格式来存储。

JSON

[
    [
        {
            "product":{
                "code":"A001",
                "description":"Almond Flavored Syrup",
                "price":"9.0"
            },
            "quantity":"3"
        },
        {
            "product":{
                "code":"A002",
                "description":"Irish Creme Flavored Syrup",
                "price":"9.0"
            },
            "quantity":"5"
        }
    ],
    [
        {
            "product":{
                "code":"A001",
                "description":"Almond Flavored Syrup",
                "price":"9.0"
            },
            "quantity":"1"
        },
        {
            "product":{
                "code":"A004",
                "description":"Caramel Flavored Syrup",
                "price":"9.0"
            },
            "quantity":"2"
        },
        {
            "product":{
                "code":"A006",
                "description":"Gourmet Coffee Travel Thermo",
                "price":"18.0"
            },
            "quantity":"3"
        }
    ],
    [
        {
            "product":{
                "code":"A009",
                "description":"Gourmet Coffee 36 Cup Filters",
                "price":"45.0"
            },
            "quantity":"100"
        }
    ]
]

JSLint验证

在这里插入图片描述

总结

本文基于之前的一份Java代码,在合理数据集的范围内,编写模拟数据的XML格式代码和JSON格式代码,并分别使用浏览器和JSLint,对其合法性加以验证。

本文内容并不复杂,所以大佬们无须喷,只是希望对需要的人有所帮助。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!