1
我使用XOM規範化一些XML。但是輸出前面會有一些奇怪的字符。代碼的核心如下:Java XOM規範化 - 僞造字符
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)。
我在做什麼錯?
我已經使用過XOM了,但是如果上面的代碼有錯誤,我無法檢測到它。你能提供你正在嘗試使用的XML(或URL)嗎?也許我可以重現你遇到的問題並從那裏開始工作? – orangepips 2010-11-01 13:19:11
感謝orangepips,XML如上面修改後的問題所示 – RedGrittyBrick 2010-11-01 17:58:49