[英]Xml Transformer give me an error when trying to transform a StaxSource into a StreamResult
嘗試將 Staxsource 轉換為 StreamResult 給我一個錯誤(在 Wildfly 24 上使用 Adoptopenjdk 11-hotspot 運行時)
但是從單元測試中執行的相同代碼沒有錯誤地通過並且正確地完成了工作(使用 Adoptopenjdk 11-hotspot)
這是我得到的錯誤:
14:50:15,455 ERROR [com.x.x.x.x.x.DeliveryParser] (default task-1) javax.xml.transform.TransformerException: Source object passed to ''{0}'' has no contents.
ERROR: 'Source object passed to ''{0}'' has no contents.'
這里的源代碼:
private static String readElementString(final XMLStreamReader streamReader) {
try {
StringWriter stringWriter = new StringWriter();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.toString());
StAXSource stAXSource = new StAXSource(streamReader);
StreamResult streamResult = new StreamResult(stringWriter);
transformer.transform(stAXSource, streamResult); <-- error is thrown here
return stringWriter.toString();
...
無法在網上找到我可以使用的東西。
有人對我有提示嗎?
Thx - 法比安
在這里我找到了解決方案:
在進行單元測試時,“TransformerFactory”給了我一個來自 JDK 的“com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl”實例。
在 Wildlfy 上,工廠給了我一個“org.apache.xalan.xsltc.trax.TransformerImpl”的實例。
當我嘗試轉換 StaxSource 時,最后一個導致我出錯。 我不知道為什么。
所以,我現在使用下面的代碼從 JDK 實例化 TransformerFactory :
TransformerFactory.newDefaultInstance();
代替
TransformerFactory.newInstance();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.