ITeye博客

XML Format xml格式化 xml pretty print

butterflymacro · 2012-08-28 17:54 · XML格式化 xml pretty print · 编程语言 · Markdown

XML pretty print,  XML format,  XML 格式化

今天在项目中用到WebService,发现有时候可能SOAP消息,可能是服务端返回或者客户端请求都有可能 不是格式化好的SOAP消息

例如

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:do_modifyUserBasicInfo xmlns:ns2="http://imsintf.service.openbilling.ailk.com/"><sOper><so_nbr>10262</so_nbr><busi_code>1033</busi_code><so_mode>2</so_mode><so_date>2012-08-28 01:20:34</so_date><charge_flag>1</charge_flag><source_system>01</source_system></sOper><sUser><phone_id>0852273049</phone_id><password>123456</password></sUser></ns2:do_modifyUserBasicInfo></soap:Body></soap:Envelope>

整个报文就一行,让人很难看到参数的具体值

项目中要求输出合格化的XML报文(Indent) 或者说是pretty print   xml树形结构.

如:

<?xml version="1.0" encoding="UTF-8"?>

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

<soap:Body>

<ns2:do_modifyUserBasicInfo xmlns:ns2="http://imsintf.service.openbilling.ailk.com/">

<sOper>

<so_nbr>10400</so_nbr>

<busi_code>1033</busi_code>

<so_mode>2</so_mode>

<so_date>2012-08-28 05:35:28</so_date>

<charge_flag>1</charge_flag>

<source_system>01</source_system>

</sOper>

<sUser>

<phone_id>0852273049</phone_id>

<password>123456</password>

</sUser>

</ns2:do_modifyUserBasicInfo>

</soap:Body>

</soap:Envelope>

 

项目中使用的Spring集成CXF,查阅CXF可以使用拦截器来实现报文的输出,在通过 XML 的TransformerFactory  Transformer 来实现XML的格式化

代码如下

public class SOAPMessageInterceptor implements SOAPHandler<SOAPMessageContext>{

 

private Logger log = LoggerFactory.getLogger(SOAPMessageInterceptor.class);

public boolean handleMessage(SOAPMessageContext context) {

// TODO Auto-generated method stub

ByteArrayOutputStream bos = new ByteArrayOutputStream();

SOAPMessage message = context.getMessage();

StringBuilder sb = new StringBuilder();

boolean isOutBound =(Boolean)context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

sb.append("***invoke WebService****   EndPoint Method "+getMethodName(context) + "\n");

if(isOutBound){

sb.append("\nInbound Message:\n");

}else{

sb.append("\nOutbound  Message:\n");

}

 

try {

message.writeTo(bos);

Transformer transformer = TransformerFactory.newInstance().newTransformer();

transformer.setOutputProperty(OutputKeys.INDENT, "yes");

ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());

Source xmlInput = new StreamSource(bis);

bos.reset();

StreamResult xmlOutput = new StreamResult(bos);

transformer.transform(xmlInput,xmlOutput);

sb.append(bos.toString());

sb.append("\n*******************************************\n");

 

}catch(Exception e){

e.printStackTrace();

}

log.info(sb.toString());

return true;

}

 

 

 protected String getMethodName(MessageContext mc)

     {

         String operationName = null;

         try

         {

            SOAPMessageContext messageContext = (SOAPMessageContext) mc;

 

            // assume the operation name is the first element

            // after SOAP:Body element

            Iterator i = messageContext.getMessage().getSOAPPart().getEnvelope().getBody().getChildElements();

            while ( i.hasNext() )

            {

                Object obj = i.next();

                if(obj instanceof SOAPElement)

                {

                    SOAPElement e = (SOAPElement) obj;

                    operationName = e.getElementName().getLocalName();

                    break;

                }

            }

         }

         catch(Exception e)

         {

             e.printStackTrace();

         }

         return operationName;

     }

public boolean handleFault(SOAPMessageContext messagecontext) {

// TODO Auto-generated method stub

return true;

}

public void close(MessageContext messagecontext) {

// TODO Auto-generated method stub

}

public Set<QName> getHeaders() {

// TODO Auto-generated method stub

return null;

}

}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。