Java : Convert formatted xml file to one line string

前端 未结 10 1431
时光取名叫无心
时光取名叫无心 2020-12-01 04:02

I have a formatted XML file, and I want to convert it to one line string, how can I do that.

Sample xml:



        
10条回答
  •  佛祖请我去吃肉
    2020-12-01 04:31

    The above solutions work if you are compressing all white space in the XML document. Other quick options are JDOM (using Format.getCompactFormat()) and dom4j (using OutputFormat.createCompactFormat()) when outputting the XML document.

    However, I had a unique requirement to preserve the white space contained within the element's text value and these solutions did not work as I needed. All I needed was to remove the 'pretty-print' formatting added to the XML document.

    The solution that I came up with can be explained in the following 3-step/regex process ... for the sake of understanding the algorithm for the solution.

    String regex, updatedXml;
    
    // 1. remove all white space preceding a begin element tag:
    regex = "[\\n\\s]+(\\<[^/])";
    updatedXml = originalXmlStr.replaceAll( regex, "$1" );
    
    // 2. remove all white space following an end element tag:
    regex = "(\\)[\\s]+";
    updatedXml = updatedXml.replaceAll( regex, "$1" );
    
    // 3. remove all white space following an empty element tag
    // ():
    regex = "(/\\>)[\\s]+";
    updatedXml = updatedXml.replaceAll( regex, "$1" );
    

    NOTE: The pseudo-code is in Java ... the '$1' is the replacement string which is the 1st capture group.

    This will simply remove the white space used when adding the 'pretty-print' format to an XML document, yet preserve all other white space when it is part of the element text value.

提交回复
热议问题