##Jackson轉換XML##java
###依賴的jar包###api
jackson-annotations-2.6.0.jar數組
jackson-core-2.6.3.jarapp
jackson-databind-2.6.3.jar.net
jackson-dataformat-xml-2.6.4.jarcode
jackson-module-jaxb-annotations-2.6.4.jarorm
stax2-api-3.1.4.jarxml
###XmlMapper和ToXmlGenerator###對象
XmlMapperblog
XmlMapper xmlMapper = new XmlMapper();
ToXmlGenerator
輸出前須要flush(),使用後須要close
ToXmlGenerator xmlGenerator = xmlMapper.getFactory().createGenerator(System.out, JsonEncoding.UTF8); if (xmlGenerator != null) { xmlGenerator.flush(); } if (!xmlGenerator.isClosed()) { xmlGenerator.close(); }
POJO
AccountBean bean = new AccountBean(); bean.setAddress("china-Guangzhou"); bean.setEmail("hoojo_@126.com"); bean.setId(1); bean.setName("hoojo");
###POJO轉換成xml###
XmlMapper
String xml = xmlMapper.writeValueAsString(bean); //或 xmlMapper.writeValue(System.out, bean);
ToXmlGenerator
xmlGenerator.writeObject(bean); 輸出:<AccountBean xmlns=""><id>1</id><name>hoojo</name><email>hoojo_@126.com</email><address>china-Guangzhou</address><birthday/></AccountBean>
###xml轉換成POJO###
String xml = "<AccountBean xmlns=\"\"><id>1</id><name>hoojo</name><email>hoojo_@126.com</email><address>china-Guangzhou</address><birthday/></AccountBean>"; AccountBean accountBean = xmlMapper.readValue(xml, AccountBean.class);
###List轉換成xml###
XmlMapper
String xmlList = xmlMapper.writeValueAsString(list); //或 xmlMapper.writeValue(System.out, list);
ToXmlGenerator
xmlGenerator.writeObject(list); <ArrayList xmlns=""><item><id>1</id><name>hoojo</name><email>hoojo_@126.com</email><address>china-Guangzhou</address><birthday/></item><item><id>1</id><name>hoojo</name><email>hoojo_@126.com</email><address>china-Guangzhou</address><birthday/></item></ArrayList>
###xml轉換成數組###
String xmlList = "<ArrayList xmlns=""><item><id>1</id><name>hoojo</name><email>hoojo_@126.com</email><address>china-Guangzhou</address><birthday/></item><item><id>1</id><name>hoojo</name><email>hoojo_@126.com</email><address>china-Guangzhou</address><birthday/></item></ArrayList>";
AccountBean[] beans = xmlMapper.readValue(xmlList,AccountBean[].class);
###xml轉換成List###
String xmlList = "<ArrayList xmlns=\"\"><item><id>1</id><name>hoojo</name><email>hoojo_@126.com</email><address>china-Guangzhou</address><birthday/></item><item><id>1</id><name>hoojo</name><email>hoojo_@126.com</email><address>china-Guangzhou</address><birthday/></item></ArrayList>"; JavaType listType = xmlMapper.getTypeFactory().constructParametrizedType(ArrayList.class, List.class, AccountBean.class); ArrayList<AccountBean> list1 = xmlMapper.readValue(xmlList, listType); for(int i=0; i<list1.size(); i++){ System.out.println(list1.get(i)); }
###xml轉換成List<Map>###
(全部的xml對象都會直接解析成LinkedHashMap)
String xmlList = "<ArrayList xmlns=\"\"><item><id>1</id><name>hoojo</name><email>hoojo_@126.com</email><address>china-Guangzhou</address><birthday/></item><item><id>1</id><name>hoojo</name><email>hoojo_@126.com</email><address>china-Guangzhou</address><birthday/></item></ArrayList>"; List<LinkedHashMap<String,Object>> list2 = xmlMapper.readValue(xmlList, List.class); for(int i=0; i<list2.size(); i++){ LinkedHashMap<String,Object> map = list2.get(i); Iterator<Map.Entry<String,Object>> entryIter = map.entrySet().iterator(); while(entryIter.hasNext()){ Map.Entry<String,Object> entry = entryIter.next(); System.out.println(entry.getKey() + "=" + entry.getValue()); } System.out.println(list2.get(i)); }
###Map轉換成xml###
XmlMapper
String mapStrrin = xmlMapper.writeValueAsString(map); //或 xmlMapper.writeValue(System.out, map);
ToXmlGenerator
xmlGenerator.writeObject(map); <HashMap xmlns=""><A><id>1</id><name>hoojo</name><email>hoojo_@126.com</email><address>china-Guangzhou</address><birthday/></A><B><id>1</id><name>hoojo</name><email>hoojo_@126.com</email><address>china-Guangzhou</address><birthday/></B></HashMap>
###xml轉換成Map###
String xmlMap = "<HashMap xmlns=\"\"><A><id>1</id><name>hoojo</name><email>hoojo_@126.com</email><address>china-Guangzhou</address><birthday/></A><B><id>1</id><name>hoojo</name><email>hoojo_@126.com</email><address>china-Guangzhou</address><birthday/></B></HashMap>";
第一種:解析成String-POJO
JavaType javaType = xmlMapper.getTypeFactory().constructMapType(Map.class, String.class, AccountBean.class); Map<String, AccountBean> maps = xmlMapper.readValue(xmlMap, javaType); Iterator<Map.Entry<String,AccountBean>> entryIter = maps.entrySet().iterator(); while(entryIter.hasNext()){ Map.Entry<String,AccountBean> entry = entryIter.next(); System.out.println(entry.getKey() + "==" + entry.getValue()); }
第二種:解析成String-LinkedHashMap<String,String>
Map<String, LinkedHashMap<String,String>> map = xmlMapper.readValue(xmlMap, Map.class); System.out.println(map); Iterator<Map.Entry<String,LinkedHashMap<String,String>>> entryIterator = map.entrySet().iterator(); while(entryIter.hasNext()){ Map.Entry<String,LinkedHashMap<String,String>> entrys = entryIterator.next(); System.out.println(entrys.getKey() + "="); LinkedHashMap<String,String> entryMap = entrys.getValue(); Iterator<Map.Entry<String,String>> iter = entryMap.entrySet().iterator(); while(iter.hasNext()){ Map.Entry<String,String> entry = iter.next(); System.out.println(entry.getKey() + "=" + entry.getValue()); } }
###複雜的xml輸出###
XmlMapper xmlMapper = new XmlMapper(); ToXmlGenerator xmlGenerator = xmlMapper.getFactory().createGenerator(System.out, JsonEncoding.UTF8); QName root = new QName("catalog"); xmlGenerator.setNextName(root);//<catalog> xmlGenerator.writeStartObject(); QName book = new QName("book"); xmlGenerator.startWrappedValue(book, root);//<book> xmlGenerator.setNextIsAttribute(true); xmlGenerator.writeFieldName("id"); xmlGenerator.writeString("bk101"); xmlGenerator.setNextIsAttribute(false); xmlGenerator.writeFieldName("title"); xmlGenerator.writeString("the title"); xmlGenerator.setNextIsAttribute(false); xmlGenerator.writeFieldName("price"); xmlGenerator.writeNumber(44.95);; xmlGenerator.finishWrappedValue(book, root);//</book> xmlGenerator.startWrappedValue(book, root);//<book> xmlGenerator.setNextIsAttribute(true); xmlGenerator.writeFieldName("id"); xmlGenerator.writeString("bk102"); xmlGenerator.setNextIsAttribute(false); xmlGenerator.writeFieldName("title2"); xmlGenerator.writeString("the title2"); xmlGenerator.setNextIsAttribute(false); xmlGenerator.writeFieldName("price2"); xmlGenerator.writeNumber(22.95);; xmlGenerator.finishWrappedValue(book, root);//</book> xmlGenerator.writeEndObject();//</catalog> xmlGenerator.flush(); xmlGenerator.close(); 輸出:(id前的zdef-863096486:暫時不知道怎麼解決) <catalog xmlns=""> <book zdef-863096486:id="bk101"> <title>the title</title> <price>44.95</price> </book> <book zdef737113102:id="bk102"> <title2>the title2</title2> <price2>22.95</price2> </book> </catalog>
###其餘###