How to pretty print XML from Java?

后端 未结 30 2921
慢半拍i
慢半拍i 2020-11-22 01:55

I have a Java String that contains XML, with no line feeds or indentations. I would like to turn it into a String with nicely formatted XML. How do I do this?



        
30条回答
  •  轮回少年
    2020-11-22 02:42

    For those searching for a quick and dirty solution - which doesn't need the XML to be 100% valid. e.g. in case of REST / SOAP logging (you never know what the others send ;-))

    I found and advanced a code snipped I found online which I think is still missing here as a valid possible approach:

    public static String prettyPrintXMLAsString(String xmlString) {
        /* Remove new lines */
        final String LINE_BREAK = "\n";
        xmlString = xmlString.replaceAll(LINE_BREAK, "");
        StringBuffer prettyPrintXml = new StringBuffer();
        /* Group the xml tags */
        Pattern pattern = Pattern.compile("(<[^/][^>]+>)?([^<]*)(]+>)?(<[^/][^>]+/>)?");
        Matcher matcher = pattern.matcher(xmlString);
        int tabCount = 0;
        while (matcher.find()) {
            String str1 = (null == matcher.group(1) || "null".equals(matcher.group())) ? "" : matcher.group(1);
            String str2 = (null == matcher.group(2) || "null".equals(matcher.group())) ? "" : matcher.group(2);
            String str3 = (null == matcher.group(3) || "null".equals(matcher.group())) ? "" : matcher.group(3);
            String str4 = (null == matcher.group(4) || "null".equals(matcher.group())) ? "" : matcher.group(4);
    
            if (matcher.group() != null && !matcher.group().trim().equals("")) {
                printTabs(tabCount, prettyPrintXml);
                if (!str1.equals("") && str3.equals("")) {
                    ++tabCount;
                }
                if (str1.equals("") && !str3.equals("")) {
                    --tabCount;
                    prettyPrintXml.deleteCharAt(prettyPrintXml.length() - 1);
                }
    
                prettyPrintXml.append(str1);
                prettyPrintXml.append(str2);
                prettyPrintXml.append(str3);
                if (!str4.equals("")) {
                    prettyPrintXml.append(LINE_BREAK);
                    printTabs(tabCount, prettyPrintXml);
                    prettyPrintXml.append(str4);
                }
                prettyPrintXml.append(LINE_BREAK);
            }
        }
        return prettyPrintXml.toString();
    }
    
    private static void printTabs(int count, StringBuffer stringBuffer) {
        for (int i = 0; i < count; i++) {
            stringBuffer.append("\t");
        }
    }
    
    public static void main(String[] args) {
        String x = new String(
                "soap:ClientINVALID_MESSAGE20007INVALID_MESSAGEProblems creating SAAJ object model");
        System.out.println(prettyPrintXMLAsString(x));
    }
    

    here is the output:

    
      
        
            soap:Client
            INVALID_MESSAGE
            
                
                    20007
                    INVALID_MESSAGE
                    Problems creating SAAJ object model
                
            
        
      
    
    

提交回复
热议问题