Java 怎么解压缩XML字符串

2019年8月3日23:24:52Java 怎么解压缩XML字符串已关闭评论 461 2546字阅读8分29秒

1.简介

Java 怎么解压缩XML字符串?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */



public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] xml = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(decompressXML(xml));
    }

    private static int endDocTag = 0x00100101;
    private static int startTag = 0x00100102;
    private static int endTag = 0x00100103;
    private static String spaces = "                                             ";

    public static String decompressXML(final byte[] xml) {

        StringBuilder finalXML = new StringBuilder();

        int numbStrings = LEW(xml, 4 * 4);

        int sitOff = 0x24; 

        int stOff = sitOff + numbStrings * 4; 
 
        int xmlTagOff = LEW(xml, 3 * 4); 

        for (int ii = xmlTagOff; ii < xml.length - 4; ii += 4) {
            if (LEW(xml, ii) == startTag) {
                xmlTagOff = ii;
                break;
            }
        } 

        int off = xmlTagOff;
        int indent = 0;
        int startTagLineNo = -2;
        while (off < xml.length) {
            int tag0 = LEW(xml, off);
            
            int lineNo = LEW(xml, off + 2 * 4);
            
            int nameNsSi = LEW(xml, off + 4 * 4);
            int nameSi = LEW(xml, off + 5 * 4);

            if (tag0 == startTag) { 
                int tag6 = LEW(xml, off + 6 * 4); 
                int numbAttrs = LEW(xml, off + 7 * 4); 
                                                       
                
                off += 9 * 4; 
                String name = compXmlString(xml, sitOff, stOff, nameSi);
                
                startTagLineNo = lineNo;

                
                StringBuffer sb = new StringBuffer();
                for (int ii = 0; ii < numbAttrs; ii++) {
                    int attrNameNsSi = LEW(xml, off); 
                                                      
                    int attrNameSi = LEW(xml, off + 1 * 4); 
                                                            
                    int attrValueSi = LEW(xml, off + 2 * 4); 
                                                             
                                                             
                    int attrFlags = LEW(xml, off + 3 * 4);
                    int attrResId = LEW(xml, off + 4 * 4); 
                                                           
                                                           
                    off += 5 * 4; 

                    String attrName = compXmlString(xml, sitOff, stOff,
                            attrNameSi);
                    String attrValue = attrValueSi != -1 ? compXmlString(
                            xml, sitOff, stOff, attrValueSi)
                            : "resourceID 0x"
                                    + Integer.toHexString(attrResId);
                    sb.append(" " + attrName + "=\"" + attrValue + "\"");
                    
                }
                finalXML.append("<" + name + sb + ">");
                prtIndent(indent, "<" + name + sb + ">");
                indent++;

            } else if (tag0 == endTag) { 
                indent--;
                off += 6 * 4; 
                String name = compXmlString(xml, sitOff, stOff, nameSi);
                finalXML.append("</" + name + ">");
                prtIndent(indent, "</" + name + "> (line " + startTagLineNo
                        + "-" + lineNo + ")");
                

            } else if (tag0 == endDocTag) { 
                break;

            } else {
                prt("  Unrecognized tag code '" + Integer.toHexString(tag0)
                        + "' at offset " + off);
                break;
            }
        } 
          
        return finalXML.toString();
    }

    private static int LEW(final byte[] arr, final int off) {
        return arr[off + 3] << 24 & 0xff000000 | arr[off + 2] << 16
                & 0xff0000 | arr[off + 1] << 8 & 0xff00 | arr[off] & 0xFF;
    }

    private static String compXmlString(final byte[] xml, final int sitOff,
            final int stOff, final int strInd) {
        if (strInd < 0) {
            return null;
        }
        int strOff = stOff + LEW(xml, sitOff + strInd * 4);
        return compXmlStringAt(xml, strOff);
    }

    private static void prtIndent(final int indent, final String str) {
        prt(spaces.substring(0, Math.min(indent * 2, spaces.length()))
                + str);
    }

    private static void prt(final String str) {
        
    }

    private static String compXmlStringAt(final byte[] arr, final int strOff) {
        int strLen = arr[strOff + 1] << 8 & 0xff00 | arr[strOff] & 0xff;
        byte[] chars = new byte[strLen];
        for (int ii = 0; ii < strLen; ii++) {
            chars[ii] = arr[strOff + 2 + ii * 2];
        }
        return new String(chars); 
    }
}

 
继续阅读
编程十万个怎么办