gpt4 book ai didi

java - 提取 EXI 压缩的 XML 时出现编码问题

转载 作者:太空宇宙 更新时间:2023-11-04 07:51:29 26 4
gpt4 key购买 nike

下面的代码尝试简化使用EXIficient执行EXI压缩和解压缩所需的设置。

class ExiCompressionUtils {
static Transformer transformer = TransformerFactory.newInstance().newTransformer()

static byte[] compress(String xml) {
ByteArrayOutputStream exiOS = new ByteArrayOutputStream()
EXIResult exiResult = new EXIResult(outputStream : exiOS)

XMLReader xmlReader = XMLReaderFactory.createXMLReader()
xmlReader.contentHandler = exiResult.handler
xmlReader.parse(new InputSource(new StringReader(xml)))

def compressed = exiOS.toByteArray()
exiOS.close()
return compressed
}

static String extract(byte[] compressed) {
SAXSource exiSource = new SAXSource(new InputSource(new ByteArrayInputStream(compressed)))
exiSource.setXMLReader(exiSource.reader)

ByteArrayOutputStream exiOS = new ByteArrayOutputStream()
transformer.transform(exiSource, new StreamResult(exiOS)) // fails here
def extracted = exiOS.toString()
exiOS.close()
return compressed
}
}

以下测试失败,并显示错误:“1 字节 UTF-8 序列的第 1 字节无效。”

@Test
void testExiCompression() {
def xml = '<Root><Child id="1">Text</Child><EmptyTag/></Root>'
def compressed = ExiCompressionUtils.compress(xml)
assert ExiCompressionUtils.extract(compressed) == xml
}

有编码专家可以深入了解这个问题吗?

最佳答案

今天我为这个评论而苦苦挣扎。这段代码有一个重要的问题(除了 Java 缺少分号等奇怪的语法)

阅读时使用 EXISource 而不是 SAXSource!

附上有效的代码段。

--丹尼尔

static Transformer transformer;

static {
try {
transformer = TransformerFactory.newInstance().newTransformer();
} catch (TransformerConfigurationException e) {
} catch (TransformerFactoryConfigurationError e) {
}
}

static byte[] compress(String xml) throws IOException, EXIException,
SAXException {
ByteArrayOutputStream exiOS = new ByteArrayOutputStream();
EXIResult exiResult = new EXIResult();
exiResult.setOutputStream(exiOS);

XMLReader xmlReader = XMLReaderFactory.createXMLReader();
xmlReader.setContentHandler(exiResult.getHandler());
xmlReader.parse(new InputSource(new StringReader(xml)));

byte[] compressed = exiOS.toByteArray();
exiOS.close();

return compressed;
}

static String extract(byte[] compressed) throws TransformerException,
IOException, EXIException {
// SAXSource exiSource = new SAXSource(new InputSource(new
// ByteArrayInputStream(compressed))); // use EXISource instead!
SAXSource exiSource = new EXISource();
exiSource.setInputSource(new InputSource(new ByteArrayInputStream(
compressed)));

ByteArrayOutputStream exiOS = new ByteArrayOutputStream();
transformer.transform(exiSource, new StreamResult(exiOS));
String extracted = exiOS.toString();
exiOS.close();
return extracted;
}

public static void main(String[] args) throws IOException, EXIException,
SAXException, TransformerException {
String xml = "<Root><Child id=\"1\">Text</Child><EmptyTag/></Root>";
byte[] compressed = ExiCompressionUtils.compress(xml);
System.out.println(ExiCompressionUtils.extract(compressed));
}

关于java - 提取 EXI 压缩的 XML 时出现编码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14388309/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com