Java XOM规范化-伪字符
我正在使用规范化一些XML。但是输出中有一些奇怪的字符。守则的核心内容如下:Java XOM规范化-伪字符,java,xml,Java,Xml,我正在使用规范化一些XML。但是输出中有一些奇怪的字符。守则的核心内容如下: String result; outputstream = new ObjectOutputStream(bytestream); Builder builder = new Builder(); Canonicalizer canonicalizer = new Canonicalizer(outputstream, Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION); nu.
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中时,在第一个
之前会显示六个意外字符。问题是,出于某种原因,我无法理解,我将ByteArrayOutputStream包装在ObjectOutputStream中。因此,可能输出的前缀是对象元数据的某种序列化
我只是直接使用了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 (...){ ... }
我曾经使用过XOM,但如果上面的代码中有错误,我无法检测到。你能提供你正在尝试使用的XML(或它的URL)吗?也许我可以重现你遇到的问题并从那里向后工作?谢谢你,XML如上面修改的问题所示