我正在使用XOM来规范化一些XML。但是输出中有一些奇怪的字符。代码的核心如下:
String result;
outputstream = new ObjectOutputStream(bytestream);
Builder builder = new Builder();
Canonicalizer canonicalizer = new Canonicalizer(outputstream, Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION);
nu.xom.Document input = builder.build(xml, uri);
Node node = input.getRootElement();
String xpath = "//a:head";
XPathContext context = new XPathContext("a", "http://example.com/a");
Nodes nodes = node.query(xpath, context);
if (nodes.size() > 0) {
canonicalizer.write(nodes.get(0));
outputstream.close();
result = bytestream.toString("UTF8");
}包含的xml
<a:envelope xmlns:b='http://example.com/b' xmlns:a="http://example.com/a">
<a:document>
<a:head>
<b:this>this</b:this>
<b:that>that</b:that>
<b:what />
</a:head>
<a:body>
</a:body>
</a:document>
</a:envelope>当结果显示在JTextarea中时,在第一个<之前会显示六个意外字符。字节流中字节的十进制值为-84、-19、0、5、119、-36、60。(后面是规范的XML)。
我做错了什么?
发布于 2010-11-02 02:03:20
问题是,由于某种原因我无法理解,我在ObjectOutputStream.中包装了一个ByteArrayOutputStream因此,假设输出带有对象元数据的一些序列化前缀。
我只是直接使用了ByteArrayOutputStream,现在的输出正如我所期望的那样。
String result = "error";
String uri = "http://example.com/uri";
String xpath = textArea.getText();
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
try {
Builder builder = new Builder();
Canonicalizer canonicalizer = new Canonicalizer(bytestream,
Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION);
nu.xom.Document input = builder.build(xml, uri);
Node node = input.getRootElement();
XPathContext context = new XPathContext("a", "http://example.com/a");
Nodes nodes = node.query(xpath, context);
if (nodes.size() > 0) {
canonicalizer.write(nodes.get(0));
bytestream.close();
result = bytestream.toString("UTF8");
}
catch (...){ ... }https://stackoverflow.com/questions/4065226
复制相似问题