Hello i need to add several elements from the Google Multimap class to Arrays in order to be able to pass them to the JAXB marshall method for marshalling.
public void marshall(Multimap<String, Product> supplierProducts) throws JAXBException { JAXBContext context = JAXBContext.newInstance(Product.class); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); Product[] panasonic = new Product[10]; Product[] apple = new Product[10]; Product[] sony = new Product[10]; for (Product product : supplierProducts.values()) { if (product.getSupplier().equals("Panasonic")) { } else if (product.getSupplier().equals("Apple")) { apple } else { sony } } marshaller.marshal(panasonic, new File("src/output/panasonic.xml")); marshaller.marshal(apple, new File("src/output/apple.xml")); marshaller.marshal(sony, new File("src/output/sony.xml")); }
In the map i have Product objects.The key is one of the 3 values:panasonic,sony or apple while the values is a product with several fields.I need to add to each array the products that contain only the specific key so that i can write to 3 sepparate xml files the products from each supplier. So the problem is that i do not know how to do this.It got really confusing really fast.Any help would be greatly appreciated !
Advertisement
Answer
you can use below approach to solve problem, Version-2:
public void marshallV1(Multimap<String, Product> supplierProducts) throws JAXBException { JAXBContext context = JAXBContext.newInstance(Product.class); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); List<Product> panasonic = new ArrayList<>(); List<Product> apple = new ArrayList<>(); List<Product> sony = new ArrayList<>(); for (Product product : supplierProducts.values()) { if (product.getSupplier().equals("Panasonic")) { panasonic.add(product ); } else if (product.getSupplier().equals("Apple")) { apple.add(product ); } else { sony.add(product ); } } marshaller.marshal(panasonic, new File("src/output/panasonic.xml")); marshaller.marshal(apple, new File("src/output/apple.xml")); marshaller.marshal(sony, new File("src/output/sony.xml")); }
Version-2:
public void marshallV2(Multimap<String, List<Product>> supplierProducts) throws JAXBException { JAXBContext context = JAXBContext.newInstance(Product.class); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); List<Product> panasonic = supplierProducts.get("panasonic "); List<Product> apple = supplierProducts.get("apple "); List<Product> sony = supplierProducts.get("sony "); marshaller.marshal(panasonic, new File("src/output/panasonic.xml")); marshaller.marshal(apple, new File("src/output/apple.xml")); marshaller.marshal(sony, new File("src/output/sony.xml")); }