Soap Message Formator

在开发webservice 的程序中,我们经常要把soap request 跟soap response要打出来查看,但是如果我们取soap body中的内容打出的化会是很长的一个xml string,非常的不可读,以下class提供的一个方法把xml string format成可读的格式.

例如

<Root><child1>test1</child1><child2>test2</chidl2></Root>

格式化以后

<Root>

   <child1>test1</child1>

  <child2>test2</child2>

 

</Root>

public class IndentFormatter { 
    private static final Pattern ELEMENT = Pattern.compile("<[^<^>]+>|[^<^>]+"); 

    private static final Pattern ELEMENT_START = Pattern.compile("<[^/][^<^>]+[^/]>"); 

    private static final Pattern ELEMENT_END = Pattern.compile("</[^<^>]+>"); 

    private static final Pattern ELEMENT_EMPTY = Pattern.compile("<[^<^>]+/>"); 

    private static final Pattern ELEMENT_TEXT = Pattern.compile("[^<^>]+"); 

    private static Map<Integer, String> indentPool = new HashMap<Integer, String>(); 

    private static final String INDENT = "  "; 

    public static String format(String xml) { 
        StringBuffer buffer = new StringBuffer(); 
        try { 

            Matcher matcher = ELEMENT.matcher(xml); 

            int level = 0; 
            String prev = null; 
            while (matcher.find()) { 
                String g = matcher.group(); 
                if (isStartElement(g)) { 
                    if (isStartElement(prev)) { 
                        level++; 
                    } 
                    if (prev != null) { 
                        buffer.append("\n"); 
                    } 
                    buffer.append(indent(level) + g); 
                } else if (isText(g)) { 
                    buffer.append(g); 
                } else if (isEmptyElement(g)) { 
                    if (isStartElement(prev)) { 
                        level++; 
                    } 
                    buffer.append("\n" + indent(level) + g); 
                } else if (isEndElement(g)) { 
                    if (isEndElement(prev) || isEmptyElement(prev)) { 
                        level--; 
                        buffer.append("\n" + indent(level)); 
                    } 
                    buffer.append(g); 
                } 
                prev = g; 
            } 
        } catch (Throwable e) { 
            return xml; 
        } 
        return buffer.toString(); 
    } 

    private static boolean isStartElement(String node) { 
        if (node == null) { 
            return false; 
        } 
        return ELEMENT_START.matcher(node).matches(); 
    } 

    private static boolean isEndElement(String node) { 
        if (node == null) { 
            return false; 
        } 
        return ELEMENT_END.matcher(node).matches(); 
    } 

    private static boolean isEmptyElement(String node) { 
        if (node == null) { 
            return false; 
        } 
        return ELEMENT_EMPTY.matcher(node).matches(); 
    } 

    private static boolean isText(String node) { 
        if (node == null) { 
            return false; 
        } 
        return ELEMENT_TEXT.matcher(node).matches(); 
    } 

    private static String indent(int level) { 
        if (level < 1) { 
            return ""; 
        } 
        String indent = indentPool.get(level); 
        if (indent == null) { 
            StringBuffer buffer = new StringBuffer(); 
            for (int i = 0; i < level; i++) { 
                buffer.append(INDENT); 
            } 
            indent = buffer.toString(); 
            indentPool.put(level, indent); 
        } 
        return indent; 
    } 

} 

 

你可能感兴趣的:(xml,webservice,SOAP)