package com.ssm.main;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.omg.CORBA.PUBLIC_MEMBER;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.ssm.utils.SpringMVCXmlUtils;
import com.ssm.utils.XmlUtils;
import com.ssm.utils.SpringMVCXmlUtils.Person;
public class TestMain {
@Test
public void testXml2BeanString(){
String xml="<?xml version=\"1.0\" encoding=\"GB2312\"?>"
+ "<person>"
+ "<address>china-Guangzhou</address>"
+ "<name>hoojo</name>"
+ "<id>1</id>"
+ "<email>hoojo_@126.com</email>"
+ "</person>";
XmlMapper xmlmapper=new XmlMapper();
try {
Person person=xmlmapper.readValue(xml,Person.class);
System.out.println("person="+person);
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testXml2BeanFile(){
String xml="<?xml version=\"1.0\" encoding=\"GB2312\"?>"
+ "<person>"
+ "<address>china-Guangzhou</address>"
+ "<name>hoojo</name>"
+ "<id>1</id>"
+ "<email>hoojo_@126.com</email>"
+ "</person>";
XmlMapper xmlmapper=new XmlMapper();
try {
String filename="d:/a.xml";
Person person=xmlmapper.readValue(new File(filename),Person.class);
System.out.println("person="+person);
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testBean2Xml(){
try {
com.ssm.main.Person person=new com.ssm.main.Person();
person.setAddress("中国");
person.setEmail("xxx@xxx.com");
person.setId(100);
person.setName("名字");
String xml2=XmlUtils.bean2Xml(person);
System.out.println("person: "+xml2);
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testJson2Xml(){
String json="{\"address\":\"中国\",\"name\":\"名字\",\"id\":1000,\"email\":\"xx@xx.com.cn\"}";
try {
String xml2=XmlUtils.json2Xml(json);
System.out.println("person: "+xml2);
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testXml2Json(){
//String json="{\"address\":\"中国\",\"name\":\"名字\",\"id\":1000,\"email\":\"xx@xx.com.cn\"}";
String xml="<?xml version=\"1.0\" encoding=\"GB2312\"?>"
+ "<person>"
+ "<address>china-Guangzhou</address>"
+ "<name>hoojo</name>"
+ "<id>1</id>"
+ "<email>hoojo_@126.com</email>"
+ "</person>";
try {
String json2=XmlUtils.xml2Json(xml);
System.out.println("person: "+json2);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testObject2Xml(){
try {
com.ssm.main.Person person1=new com.ssm.main.Person();
person1.setAddress("中国");
person1.setEmail("xxx@xxx.com.cn");
person1.setId(100);
person1.setName("中国名字");
com.ssm.main.Person person2=new com.ssm.main.Person();
person2.setAddress("日本");
person2.setEmail("xxx@xxx.com.jp");
person2.setId(100100);
person2.setName("日本名字");
List<com.ssm.main.Person> list=new ArrayList<com.ssm.main.Person>();
list.add(person1);
list.add(person2);
XmlMapper xmlmapper=new XmlMapper();
String xml=xmlmapper.writeValueAsString(list);
System.out.println("person: "+xml);
System.out.println("==============map========================");
Map<String, com.ssm.main.Person> map=new HashMap<String, com.ssm.main.Person>();
map.put("person1", person1);
map.put("person2",person2);
String xml2=xmlmapper.writeValueAsString(map);
System.out.println(xml2);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testSpringMVCXmlUtils(){
SpringMVCXmlUtils springMVCXmlUtils=new SpringMVCXmlUtils();
List<SpringMVCXmlUtils.Person> persons=new ArrayList<SpringMVCXmlUtils.Person>();
SpringMVCXmlUtils.Person person1=springMVCXmlUtils.new Person();
person1.setAddress("中国");
SpringMVCXmlUtils.Email email=springMVCXmlUtils.new Email();
email.setEmail("xxx@xxx.com.cn");
List<SpringMVCXmlUtils.Email> emails=new ArrayList<SpringMVCXmlUtils.Email>();
emails.add(email);
person1.setEmails(emails);
person1.setId(100);
person1.setName("中国名字");
SpringMVCXmlUtils.Person person2=springMVCXmlUtils.new Person();
person2.setAddress("日本");
SpringMVCXmlUtils.Email email2=springMVCXmlUtils.new Email();
email2.setEmail("xxx@xxx.com.jp");
List<SpringMVCXmlUtils.Email> emails2=new ArrayList<SpringMVCXmlUtils.Email>();
emails2.add(email);
person2.setEmails(emails);
person2.setId(100100);
person2.setName("日本名字");
persons.add(person1);
persons.add(person2);
springMVCXmlUtils.setPersons(persons);
System.out.println(springMVCXmlUtils.toString());
}
}
评论1
最新资源