1

Подскажите пожалуйста, как можно сохранить порядок переменных при конвертации в xml файл.

@XmlRootElement
public class Product {
    private String productName;
    private long id;
    private String description;
    private BigDecimal price;    
    public Product() {
        this.productName = productName;
        this.id = id;
        this.description = description;
        this.price = price;
    }    
    private Product(Builder builder) {
        this.productName = builder.productName;
        this.id = builder.id;
        this.description = builder.description;
        this.price = builder.price;
    }    
    public static Builder newBuilder() {return new Builder();}    
    public String getProductName() {return productName;}    
    public void setProductName(String productName) { this.productName = productName;}    
    public long getId() {return id;}    
    public void setId(long id) {this.id = id;}    
    public String getDescription() {return description;}    
    public void setDescription(String description) {this.description = description;}

    public BigDecimal getPrice() {return price;}    
    public void setPrice(BigDecimal price) {this.price = price;}    
    public static final class Builder {   
        private String productName;
        private long id;
        private String description;
        private BigDecimal price;

        public Builder() {
        }

        public Builder productName(String val) {
            productName = val;
            return this;}    
        public Builder id(long val) {
            id = val;
            return this;}    
        public Builder description(String val) {
            description = val;
            return this;}    
        public Builder price(BigDecimal val) {
            price = val;
            return this;}

        public Product build() {return new Product(this);}    
    }

    @Override
    public String toString() {
        return "Product: " +
                "productName = " + productName +
                ", id = " + id +
                ", description = " + description +
                ", price = " + price;
    }
}
public class JAXBConverter {
    public static void main(String[] args) {
        JAXBConverter jaxbConverter = new JAXBConverter();
        jaxbConverter.marshall();
    }    
    private void marshall() {
        Product product = new Product();
        product.setProductName("Product");
        product.setId(0);
        product.setDescription("Some description");
        product.setPrice(BigDecimal.valueOf(29));    
        try {
            File file = new File("file.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(Product.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);    
            jaxbMarshaller.marshal(product, file);
            jaxbMarshaller.marshal(product, System.out);
        } catch (JAXBException e) {
            System.out.println(e.getMessage());
        }
    }
}

На выходе вот это:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<product>
    <description>Some description</description>
    <id>0</id>
    <price>29</price>
    <productName>Product</productName>
</product>

а хотелось бы

            <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <product>
    <productName>Product</productName>
    <id>0</id>
    <description>Some description</description>
    <price>29</price>
    </product>

1 ответ 1

2

Кажется, нашёлся ответ на мой вопрос:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "product")
public class Product {
    private String productName;
    private long id;
    private String description;
    private BigDecimal price;

    public Product() {
    }

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.