File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
common-core/src/main/java/com/yz/common/core/utils Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 55import org .dom4j .DocumentException ;
66import org .dom4j .DocumentHelper ;
77import org .dom4j .Element ;
8+ import org .dom4j .io .OutputFormat ;
89import org .dom4j .io .SAXReader ;
10+ import org .dom4j .io .XMLWriter ;
911
12+ import java .io .IOException ;
1013import java .io .InputStream ;
14+ import java .io .StringReader ;
15+ import java .io .StringWriter ;
1116import java .util .*;
1217
1318/**
@@ -83,4 +88,35 @@ public static String toXml(Map<String,String> map){
8388 sb .append ("</xml>" );
8489 return sb .toString ();
8590 }
91+
92+ /**
93+ * xml格式化
94+ * @param inputXML
95+ * @return
96+ * @throws Exception
97+ */
98+ public String formatXML (String inputXML ) throws Exception {
99+ SAXReader reader = new SAXReader ();
100+ Document document = reader .read (new StringReader (inputXML ));
101+ String requestXML = null ;
102+ XMLWriter writer = null ;
103+ if (document != null ) {
104+ try {
105+ StringWriter stringWriter = new StringWriter ();
106+ OutputFormat format = new OutputFormat (" " , true );
107+ writer = new XMLWriter (stringWriter , format );
108+ writer .write (document );
109+ writer .flush ();
110+ requestXML = stringWriter .getBuffer ().toString ();
111+ } finally {
112+ if (writer != null ) {
113+ try {
114+ writer .close ();
115+ } catch (IOException e ) {
116+ }
117+ }
118+ }
119+ }
120+ return requestXML ;
121+ }
86122}
You can’t perform that action at this time.
0 commit comments