问题
I'm looking for a fast way to linearize an XML in JAVA
I'm using ~2GB file so DOM is excluded. Java targhet is 1.5.0.22 I have to generate from an xml a file composed of 80bytes + newline. I have to write this in a DB2 table that will be read by a Cobol program.
In Cobol is important the size because the data are read as CHAR from table this implies the an empty rows is 80 spaces.
I read the file byte to byte(I must) but I can use internal temp bufferization to store the probably sequence to ignore
Example 5 bytes af ascii file
<a><b><c>psofpisogiosigpsfiogpo</c></b></a>
<a><b
><c>p
sofpi
sogio
sigps
fiogp
o</c>
</b><
/a>
PROBLEM WITH A FILE
<a>
<b>
<c>psofpisogiosigpsfiogpo</c>
</b>
</a>
<a>
<b
>
<c>ps
ofpis
ogios
igpsf
iogpo
</c>
<
/b>
</
a>
The non linearized XML create empty rows in table or some rows that ar not used to their full potential.
This became a lost of payd cpu cycle unde HOST CICS enviorment
If I can linearize the file i get same output if the file is indented or not and XML keep the same informations
Any Idea?
回答1:
private static final String XML_LINARIZATION_REGEX = "(>|>){1,1}(\\t)*(\\n|\\r)+(\\s)*(<|<){1,1}";
private static final String XML_LINARIZATION_REPLACEMENT = "$1$5";
public static String linarizeXml(String xml) {
return (xml!= null) ? xml.trim().replaceAll(XML_LINERIZATION_REGEX, XML_LINERIZATION_REPLACEMENT) : null;
}
来源:https://stackoverflow.com/questions/18445152/unindent-or-linearize-xml