在开发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;
}
}
分享到:
相关推荐
- **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 ...
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 ...
compatibility with older or less security conscience applications. We ; recommending using the production ini in production and testing environments. ; ...
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)......
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_...