I have a xml file which i read and inserted certain values into hasmap . ex: key-apple, value:111,USD,iphone4
I need to Iterate through keys and make separate xml files for each diferent name and insert into each file values from array list:
from above example it would look like:
<products>
<product>
<price>111</price>
<currency>USD</currency>
<type>iphone4</type>
<product>
<products>
BELOW IS MY CODE SO FAR>>>>>
https://pastebin.com/unqZjSRc
I have a problem with the last part in which i have to create the xml...according to values in array...can anyone suggest an idea?
output from hashmap is :
Sony
Sony 54.6" (Diag) Xbr Hx929 Internet Tv
00027242816657
2999.99
USD
2343
Sony NWZ-E464 8GB E Series Walkman Video MP3 Player Blue
00027242831438
91.99
USD
2343
Apple
Apple iPad 2 with Wi-Fi 16GB - iOS 5 - Black
00885909464517
399.0
USD
2343
Apple MacBook Air A 11.6" Mac OS X v10.7 Lion MacBook
00885909464043
1149.0
USD
2344
Panasonic
Panasonic TC-L47E50 47" Smart TV Viera E50 Series LED HDTV
00885170076471
999.99
USD
2344
Meanwhile i used:
package pss;
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<String> prods;
public ProdsList(){
prods=new ArrayList<String>();
}
public ProdsList(ArrayList<String> value){
prods=new ArrayList<String>(value);
}
public ArrayList<String> getProducts() {
return prods;
}
public void setProducts(ArrayList<String> prods) {
this.prods = prods;
}
}
with the code:
try {
JAXBContext jaxbContext = JAXBContext.newInstance(ProdsList.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Set setOfKeys = hashmap.keySet();
Iterator iterator = setOfKeys.iterator();
while (iterator.hasNext()) {
String keys = (String) iterator.next();
String filename= keys+last2digits+".xml";
File file = new File(filename);
ArrayList<String> value = hashmap.get(keys);
jaxbMarshaller.marshal(new ProdsList(value), file);
jaxbMarshaller.marshal(new ProdsList(value), System.out);
}
} catch (JAXBException e) {
e.printStackTrace();
}
Example of one output is :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Products>
<Product>Apple iPad 2 with Wi-Fi 16GB - iOS 5 - Black</Product>
<Product>00885909464517</Product>
<Product>399.0</Product>
<Product>USD</Product>
<Product>2343</Product>
<Product>Apple MacBook Air A 11.6" Mac OS X v10.7 Lion MacBook</Product>
<Product>00885909464043</Product>
<Product>1149.0</Product>
<Product>USD</Product>
<Product>2344</Product>
<products>Apple iPad 2 with Wi-Fi 16GB - iOS 5 - Black</products>
<products>00885909464517</products>
<products>399.0</products>
<products>USD</products>
<products>2343</products>
<products>Apple MacBook Air A 11.6" Mac OS X v10.7 Lion MacBook</products>
<products>00885909464043</products>
<products>1149.0</products>
<products>USD</products>
<products>2344</products>
</Products>
I'm using now: https://pastebin.com/irHtbWzi
amd still get the :
Sony_54_6___Diag__Xbr_Hx929_Internet_Tv
00027242816657
Exception in thread "main" org.w3c.dom.DOMException: INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified.
When i parse the first one it works but for the second it won't
OK, I'm not sure it's the end of the road, but the output I got is this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Products>
<Product>
<Supplier>Apple</Supplier>
<Price>10.5</Price>
<Currency>USD</Currency>
<Type>MacBook</Type>
</Product>
<Product>
<Supplier>NOKIA</Supplier>
<Price>12.5</Price>
<Currency>USD</Currency>
<Type>PHONE</Type>
</Product>
</Products>
Class Product :
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.*;
@XmlRootElement(name="Product")
@XmlAccessorType(XmlAccessType.FIELD)
public class Product {
@XmlElement(name="Supplier")
private String supplier;
@XmlElement(name="Price")
private String price;
@XmlElement(name="Currency")
private String currency;
@XmlElement(name="Type")
private String type;
public Product() {
}
public Product(String supplier, String price, String currency, String type) {
this.supplier = supplier;
this.price = price;
this.currency = currency;
this.type = type;
}
}
Class ProductList :
import javax.xml.bind.annotation.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static javax.xml.bind.JAXB.marshal;
@XmlRootElement(name="Products")
public class ProdsList {
@XmlElement(name="Product")
private ArrayList<Product> prods;
public ProdsList() {}
public ProdsList(ArrayList<Product> value){
prods = value;
}
public ArrayList<Product> getProd() {
return prods;
}
public void setProducts(ArrayList<Product> prod) {
this.prods = prod;
}
}
Main method:
public static void main(String[] args) {
Product p1 = new Product("Apple", "10.5", "USD", "MacBook");
Product p2 = new Product("NOKIA", "12.5", "USD", "PHONE");
ArrayList<Product> a = new ArrayList<>();
a.add(p1);
a.add(p2);
ProdsList l = new ProdsList(a);
marshal(l, System.out);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.