XmlUtils.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package com.emato.cuspay.util;
  2. import org.xml.sax.Attributes;
  3. import org.xml.sax.InputSource;
  4. import org.xml.sax.SAXException;
  5. import org.xml.sax.XMLReader;
  6. import org.xml.sax.helpers.XMLFilterImpl;
  7. import javax.xml.bind.JAXBContext;
  8. import javax.xml.bind.Marshaller;
  9. import javax.xml.bind.Unmarshaller;
  10. import javax.xml.parsers.SAXParserFactory;
  11. import javax.xml.transform.Source;
  12. import javax.xml.transform.sax.SAXSource;
  13. import java.io.File;
  14. import java.io.StringReader;
  15. import java.io.StringWriter;
  16. /**
  17. * XML处理类
  18. *
  19. * @author Scott Chen
  20. * @date 2016/11/15
  21. */
  22. public class XmlUtils {
  23. public static String toXML(Object obj, boolean format) throws Exception {
  24. try {
  25. JAXBContext context = JAXBContext.newInstance(obj.getClass());
  26. Marshaller marshaller = context.createMarshaller();
  27. //编码格式
  28. marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
  29. // 是否格式化生成的xml串
  30. if (format) {
  31. marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
  32. }else{
  33. marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
  34. }
  35. // 是否省略xm头声明信息
  36. marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
  37. StringWriter writer = new StringWriter();
  38. marshaller.marshal(obj, writer);
  39. return writer.toString();
  40. } catch (Exception e) {
  41. throw new RuntimeException(e);
  42. }
  43. }
  44. public static String toXMLInFile(Object obj, String filePath, boolean format) throws Exception {
  45. try {
  46. JAXBContext context = JAXBContext.newInstance(obj.getClass());
  47. Marshaller marshaller = context.createMarshaller();
  48. //编码格式
  49. marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
  50. // 是否格式化生成的xml串
  51. if (format) {
  52. marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
  53. }else{
  54. marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
  55. }
  56. // 是否省略xm头声明信息
  57. marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
  58. File file = new File(filePath);
  59. marshaller.marshal(obj, file);
  60. StringWriter writer = new StringWriter();
  61. marshaller.marshal(obj, writer);
  62. return writer.toString();
  63. } catch (Exception e) {
  64. throw new RuntimeException(e);
  65. }
  66. }
  67. @SuppressWarnings("unchecked")
  68. public static <T> T fromXML(String xmlStr, Class<T> valueType, boolean ignoreNs) throws Exception {
  69. try {
  70. JAXBContext context = JAXBContext.newInstance(valueType);
  71. Unmarshaller unmarshaller = context.createUnmarshaller();
  72. StringReader reader = new StringReader(xmlStr);
  73. //解析时,是否忽略命名空间
  74. if (ignoreNs) {
  75. /**
  76. * 忽略命名空间
  77. * 在序列化和反序列化时通过XMLFilterImpl的匿名实现类实现命名空间及xml节点名称的控制
  78. */
  79. SAXParserFactory sax = SAXParserFactory.newInstance();
  80. //false:忽略命名空间
  81. sax.setNamespaceAware(false);
  82. XMLReader xmlReader = sax.newSAXParser().getXMLReader();
  83. XMLFilterImpl nsfFilter = new XMLFilterImpl() {
  84. private boolean ignoreNamespace = false;
  85. @Override
  86. public void startDocument() throws SAXException {
  87. super.startDocument();
  88. }
  89. @Override
  90. public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
  91. if (this.ignoreNamespace){
  92. uri = "";
  93. }
  94. super.startElement(uri, localName, qName, atts);
  95. }
  96. @Override
  97. public void endElement(String uri, String localName, String qName) throws SAXException {
  98. if (this.ignoreNamespace){
  99. uri = "";
  100. }
  101. super.endElement(uri, localName, localName);
  102. }
  103. @Override
  104. public void startPrefixMapping(String prefix, String url) throws SAXException {
  105. if (!this.ignoreNamespace){
  106. super.startPrefixMapping("", url);
  107. }
  108. }
  109. };
  110. nsfFilter.setParent(xmlReader);
  111. Source source = new SAXSource(xmlReader, new InputSource(reader));
  112. return (T) unmarshaller.unmarshal(source);
  113. }
  114. return (T) unmarshaller.unmarshal(reader);
  115. } catch (Exception e) {
  116. throw new RuntimeException(e);
  117. }
  118. }
  119. }