xml格式化工具有哪些 xml文件格式化
2023-08-23
更新時間:2023-08-23 00:10:10作者:佚名
使用的javax這個jar下的類,JAXB(Java Architecture for XML Binding) 這個java和xml的互轉(zhuǎn)標準如果感興趣可以自行百度這里就不介紹概念,直接上代碼。
首先是工具類,這里比較簡單,就沒做生成實體文件,就是兩個,一個對象轉(zhuǎn)xml,一個xml轉(zhuǎn)對像。
import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import java.io.StringReader; import java.io.StringWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @desc: xml 工具類 * @author: wangzhh * @date: 2019-06-13 */ public class XmlUtils { /** * Description bean對像轉(zhuǎn)換為xml字符串, * 對像屬性如果不設置值,xml字符串沒有節(jié)點,如果需要空節(jié)點,可以設置為空字符串 * @param obj 需要轉(zhuǎn)換的對像,轉(zhuǎn)換的對像需要做對應的注解 * @return java.lang.String * @Author wangzhh * @Date 2019-06-13 **/ public static String convertToXml(Object obj) { // 輸出流 StringWriter writer = new StringWriter(); try { JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller marshaller = context.createMarshaller(); // 格式化xml輸出的格式 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); //marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE); // 設置編碼集 marshaller.setProperty(Marshaller.JAXB_ENCODING, "utf-8"); // 將對象轉(zhuǎn)換成輸出流形式的xml marshaller.marshal(obj, writer); } catch (Exception e) { e.printStackTrace(); } return writer.toString(); } /** * Description xml字符串轉(zhuǎn)換為bean對像 * @Author wangzhh * @Date 2019-06-13 * @param clazz * @param xmlStr * @return java.lang.Object **/ public static Object convertXmlStrToObject(Class clazz, String xmlStr) { Object xmlObject = null; try { JAXBContext context = JAXBContext.newInstance(clazz); // 進行將Xml轉(zhuǎn)成對象 Unmarshaller unmarshaller = context.createUnmarshaller(); StringReader reader = new StringReader(xmlStr); xmlObject = unmarshaller.unmarshal(reader); } catch (Exception e) { e.printStackTrace(); } return xmlObject; } public static void main(String[] args) { TestXmlBean t = new TestXmlBean(); t.setName("張三"); t.setAge(""); Map<String,Object> map = new HashMap<String, Object>(); map.put("map","mapValue"); t.setMsgMap(map); List<TestListBean> listBean = new ArrayList<TestListBean>(); TestListBean t1 = new TestListBean(); t1.setAddress("一號地址"); TestListBean t2 = new TestListBean(); t2.setAddress("二號地址"); listBean.add(t1); listBean.add(t2); t.setListBean(listBean); System.out.println(XmlUtils.convertToXml(t)); String xmlStr = XmlUtils.convertToXml(t); xmlStr = "<?xml version=\"1.0\" encoding=\"utf-8\"?><test><name>張三</name><age></age><msgMap><entry><key>map</key><value>mapValue</value></entry></msgMap><listBean><address>一號地址</address></listBean><listBean><address>二號地址</address></listBean></test>\n"; TestXmlBean bean = (TestXmlBean) XmlUtils.convertXmlStrToObject(TestXmlBean.class,xmlStr); System.out.println(bean.toString()); } }
測試用的類,飽含集合
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; import java.io.Serializable; import java.util.List; import java.util.Map; /** * @desc: 工具類測試對像 * @author: wangzhh * @date: 2019-06-13 */ @XmlAccessorType(XmlAccessType.FIELD) // 根節(jié)點 @XmlRootElement(name = "test") // 需要轉(zhuǎn)換的屬性,順序 @XmlType(propOrder = {"name","age","msgMap","listBean"}) public class TestXmlBean implements Serializable { private static final long serialVersionUID = -6854694430588003755L; private String name; private String age; private Map<String,Object> msgMap; private List<TestListBean> listBean; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public Map<String, Object> getMsgMap() { return msgMap; } public void setMsgMap(Map<String, Object> msgMap) { this.msgMap = msgMap; } public List<TestListBean> getListBean() { return listBean; } public void setListBean(List<TestListBean> listBean) { this.listBean = listBean; } @Override public String toString() { return "TestXmlBean{" + "name='" + name + '\'' + ", age='" + age + '\'' + ", msgMap=" + msgMap + ", listBean=" + listBean + '}'; } }
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; /** * @desc: * @author: wangzhh * @date: 2019-06-13 */ @XmlAccessorType(XmlAccessType.FIELD) // 根節(jié)點 @XmlRootElement(name = "listBean") // 需要轉(zhuǎn)換的屬性 @XmlType(propOrder = {"address"}) public class TestListBean { private String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "TestListBean{" + "address='" + address + '\'' + '}'; } }
測試結(jié)果,控制臺成功輸出
Connected to the target VM, address: '127.0.0.1:49443', transport: 'socket' <?xml version="1.0" encoding="utf-8" standalone="yes"?> <test> <name>張三</name> <age></age> <msgMap> <entry> <key>map</key> <value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">mapValue</value> </entry> </msgMap> <listBean> <address>一號地址</address> </listBean> <listBean> <address>二號地址</address> </listBean> </test> TestXmlBean{name='張三', age='', msgMap={map=[value: null]}, listBean=[TestListBe