@Test void obj2Xml() throws JAXBException, UnsupportedEncodingException { List<String> names = new ArrayList<>(); names.add("123"); names.add("123"); names.add("123"); names.add("123"); Person person = Person.builder() .name("xiaoming/name>aadsadsa]") .dsaa(names) .sex("nam") .build(); List<Person> people = new ArrayList<>(); people.add(person); people.add(person); people.add(person); people.add(person); String result = JAXBUtil.getInstance().obj2Xml(Person1.builder().person(people).build()); System.out.println(result); }
public String obj2Xml(Object obj) throws JAXBException, UnsupportedEncodingException { ByteArrayOutputStream os = new ByteArrayOutputStream(); JAXBContext jaxbContext = JAXBContext.newInstance(obj.getClass()); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true ); jaxbMarshaller.marshal(obj,os); return new String(os.toByteArray(),"UTF-8"); }
JAXBUtil { public String obj2Xml(Object obj) throws JAXBException, UnsupportedEncodingException { ByteArrayOutputStream os = new ByteArrayOutputStream(); JAXBContext jaxbContext = JAXBContext.newInstance(obj.getClass()); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true ); jaxbMarshaller.marshal(obj,os); return new String(os.toByteArray(),"UTF-8"); } }
JAXBUtil { public String obj2Xml(Object obj) throws JAXBException, UnsupportedEncodingException { ByteArrayOutputStream os = new ByteArrayOutputStream(); JAXBContext jaxbContext = JAXBContext.newInstance(obj.getClass()); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true ); jaxbMarshaller.marshal(obj,os); return new String(os.toByteArray(),"UTF-8"); } private  JAXBUtil(); }
JAXBUtil { public String obj2Xml(Object obj) throws JAXBException, UnsupportedEncodingException { ByteArrayOutputStream os = new ByteArrayOutputStream(); JAXBContext jaxbContext = JAXBContext.newInstance(obj.getClass()); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true ); jaxbMarshaller.marshal(obj,os); return new String(os.toByteArray(),"UTF-8"); } private  JAXBUtil(); static JAXBUtil getInstance(); String obj2Xml(Object obj); T xml2Obj(String xmlSource, Class<T> clazz); }
JAXBUtil { public String obj2Xml(Object obj) throws JAXBException, UnsupportedEncodingException { ByteArrayOutputStream os = new ByteArrayOutputStream(); JAXBContext jaxbContext = JAXBContext.newInstance(obj.getClass()); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true ); jaxbMarshaller.marshal(obj,os); return new String(os.toByteArray(),"UTF-8"); } private  JAXBUtil(); static JAXBUtil getInstance(); String obj2Xml(Object obj); T xml2Obj(String xmlSource, Class<T> clazz);  }