`
elicer
  • 浏览: 134528 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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; 
    } 

} 

 

分享到:
评论
2 楼 jelver 2009-12-18  
有个小bug,就是不能对<ID>元素解析,解析完后开始标签丢失掉   
public static void main(String[] args) {
       String str = "<Msgtype>advertise</Msgtype><ID>idtest</ID>         <UserID>+8613912345678</UserID>";
    	System.out.println(format(str));
    }

打印结果如下:

<Msgtype>advertise</Msgtype>idtest</ID>
<UserID>+8613912345678</UserID>

修正部分:
            while (matcher.find()) { 
                String g = matcher.group();                 
                // 原来的代码 if (isStartElement(g))){
                // 修改后的
                if (isStartElement(g) || (!isText(g) && !isEndElement(g) && "<id>".equalsIgnoreCase(g)) ) {  
                    if (isStartElement(prev)) { 
                        level++; 
                    } 
                    if (prev != null) { 
                        buffer.append("\n"); 
                    } 
                    buffer.append(indent(level) + g); 
                } else if (isText(g)) {
                ...... 
                }
            }


修正后打印结果如下:
<Msgtype>advertise</Msgtype>
<ID>idtest</ID>
<UserID>+8613912345678</UserID>
1 楼 itstarting 2009-11-26  
感觉就不是专门针对简单的SOAP消息了
对于XML-LIKE的文本,都能格式化

没试过

相关推荐

    xi tuningPerformance Tuning Checks in SAP Exchange Infrastructure

    - **Conversion:** The message is converted from its native XML format to the specific SOAP XML format with a message header attached. - **Purpose:** - **Standardization:** Standardizes the message ...

    Clever Internet Suite (SRC) v9.1.0.0

    In the new version 9.1 we have updated the MailMessage, SoapMessage and SFtp components, fixed issues in the TLS and cryptography engines. Starting from the version 8.0 the library was splitted on ...

    php.ini-development

    compatibility with older or less security conscience applications. We ; recommending using the production ini in production and testing environments. ; ...

    EurekaLog_7.5.0.0_Enterprise

    1)....Fixed: Confusing message in Manage tool when using with Trial/Pro 2)....Fixed: Range check error in processes information for x64 machines (affects startup of any EurekaLog-enabled module) 3)......

    ZendFramework中文文档

    14.5.4. Using Metacommands to Control Filter or Validator Rules 14.5.4.1. The FIELDS metacommand 14.5.4.2. The PRESENCE metacommand 14.5.4.3. The DEFAULT_VALUE metacommand 14.5.4.4. The ALLOW_...

Global site tag (gtag.js) - Google Analytics