java - 在 Java 中从 hashmap 创建多个 XML 文件

标签 java xml jaxb

我正在尝试从 HashMap 创建 XML 文件。对于哈希的每个键,我想要一个 XML 文件。键的值是对象的ArrayList。我正在使用 JAXB,但未创建 XML 文件,因为输出的 XML 无效。

对象类:

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Product")
public class Product implements Comparable<Product>{
    String ID,description, gtin;
    double price;
    String date;
    Product()
    {

    }
    public String toString()
    {
        return ID+" "+description+" "+gtin+" "+price+" "+date;
    }
    public String getID() {
        return ID;
    }
    @XmlElement
    public void setID(String ID) {
        this.ID = ID;
    }

    public String getDescription() {
        return description;
    }
    @XmlElement
    public void setDescription(String description) {
        this.description = description;
    }

    public String getGtin() {
        return gtin;
    }
    @XmlElement
    public void setGtin(String gtin) {
        this.gtin = gtin;
    }

    public double getPrice() {
        return price;
    }
    @XmlElement
    public void setPrice(Double price) {
        this.price = price;
    }
}

我尝试创建 XML 的类:

 import java.io.File;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Set;

    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.JAXBException;
    import javax.xml.bind.Marshaller;

    public class CreateXML {
        static void create(HashMap<String, ArrayList<Product> > map) {

          try {

                JAXBContext jaxbContext = JAXBContext.newInstance(ProdsList.class);
                Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

                jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
              Set setOfKeys = map.keySet();
              Iterator iterator = setOfKeys.iterator();
             while (iterator.hasNext()) {
             String keys = (String) iterator.next();
             String filename= "C:\\Users\\As\\Desktop\\Sups\\"+keys+22+".xml";
             File file = new File(filename);
              ArrayList<Product> value = map.get(keys);
            jaxbMarshaller.marshal(value, file);
            jaxbMarshaller.marshal(value, System.out);
             }
              } catch (JAXBException e) {
            e.printStackTrace();
              }

        }
    }

xml 根的类:

import java.util.*;

    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlSeeAlso;


    //@XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement(name="Products")
    //@XmlSeeAlso({ArrayList.class})
    class ProdsList {

         @XmlElement(name="Product")
         ArrayList<Product>  prods;

         public ProdsList(){
                prods=new ArrayList<Product>();
            }
         public ArrayList<Product> getProducts() {
             return prods;
         }

         public void setProducts(ArrayList<Product> prods) {
             this.prods = prods;
         }
    }

我该如何解决这个问题。提前致谢。

最佳答案

您需要编码(marshal) ProdsList 的实例。相反,你正在尝试编码 产品的 ArrayList。

改变

jaxbMarshaller.marshal(value, file);
jaxbMarshaller.marshal(value, System.out);

jaxbMarshaller.marshal(new ProdsList(value), file);
jaxbMarshaller.marshal(new ProdsList(value), System.out);

关于java - 在 Java 中从 hashmap 创建多个 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38380027/

相关文章:

java - 为什么 setLastModified(time) 不适用于此文件?

java - @TestConfiguration 在顶级类上的目的是什么?

.net - 查找任意数量的子节点具有特定属性的子节点?

json - Jersey 在 Spring 中实现 ContextResolver<JAXBContext>

jaxb - WSDL 自定义 : XMLGregorianCalender to java. util.Date

java - 从 UTF-8 格式的字符串中提取双字节字符/子字符串

java - 如何在Java中创建一个像JOptionPane.showMessageDialog()这样的JFrame对话框等待按下按钮?

sql-server - 在 SQL 2005 中使用 XML.modify() 在多个 XML 节点中插入属性

mysql - 数据库中的多个项目

java - JAXB 自定义和列表<Object>