我正在尝试使用Rhino中的SOAP web服务响应和基于文件的XSLT的输入来执行ServerSide转换。我的JAVA/JS经验很少(但在不断增长(。寻找一些关于如何将使用基于文件的xml输入找到的示例转换为w/s SOAP流的指针。
我的限制:
- 转换必须运行ServerSide,而不是在客户端
- 必须在Rhino内完成
- 传入的XML包含在Web服务(SOAP(响应中,我正在成功地将该响应放入JS
- XSLT在本地文件中
我已经能够跟踪以下源/指针,但无法确定如何从我的服务器获得xmlsoap输入,而不是像示例中那样从文件中获得。
http://simonkissane.blogspot.ca/2013/06/xml-pretty-printing-and-xslt-processing.html
我的JS非常简单,请注意,获得w/s响应的部分使用Jaggeryjs.org,它只是Rhino
我的soap响应有效,print(getBeerList(((返回我期望的xml。
我失败了:var dsDoc=new javax.xml.transform.dom.DOMSource(doc(;在函数applyXSLT中出现以下错误:
错误{org.jaggeryjs.scriptengine.engine.RinoEngine}-org.mozilla.javascript.JavaScriptException:内部错误:找不到参数为"string"的"javax.xml.transform.dom.DOMSource"的Java构造函数。(/TestJaggery_1//index.jag#90((/TestJaggery_1/index.jag#108(
JS代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
//stuff removed
</head>
<body>
<!-- Le javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/util.js"></script>
</body>
<%
function getBeerList() {
var log = new Log();
var ws = require('ws');
var mylist = new ws.WSRequest();
var options = new Array();
options.useSOAP = 1.2;
options.useWSA = 1.0;
options.action = "getAll";
var payload = null;
var result;
try {
mylist.open(options, "http://192.168.1.204:9764/services/TestSheet/", false);
mylist.send(payload);
result = mylist.responseText;
} catch (e)
{
log.error(e.toString());
print(mylist.error)
return e.toString();
}
return result;
}
print(getBeerList());
xmlToString = function(doc) {
var domreg = org.w3c.dom.bootstrap.DOMImplementationRegistry.newInstance();
var ls = domreg.getDOMImplementation("LS");
var w = ls.createLSSerializer();
w.getDomConfig().setParameter("format-pretty-print", true);
var lsout = ls.createLSOutput();
var out = new java.io.StringWriter();
lsout.setCharacterStream(out);
w.write(doc, lsout);
out.close();
return "" + out.toString();
};
getDocBuilder = function() {
var dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
dbf.setNamespaceAware(true);
return dbf.newDocumentBuilder();
};
loadXMLFile = function(file) {
var inp = new java.io.File(file);
return getDocBuilder().parse(inp);
};
applyXSLT = function(doc, xslt) {
//the following line is failing
var dsDoc = new javax.xml.transform.dom.DOMSource(doc);
var dsXSLT = new javax.xml.transform.dom .DOMSource(xslt);
var tf = javax.xml.transform.TransformerFactory.newInstance();
var xf = tf.newTransformer(dsXSLT);
var out = getDocBuilder().newDocument();
var drOut = new javax.xml.transform.dom.DOMResult(out);
xf.transform(dsDoc, drOut);
return out;
};
try {
//doc is where I am unsure of how to insert my SOAP response 'getBeerList()'
var doc = getBeerList();
var xslt = loadXMLFile(PathtoMyLocalFile);
var out = applyXSLT(doc, xslt);
println(xmlToString(out));
} catch (e) {
if (e.javaException)
e.javaException.printStackTrace();
throw e;
}
%>
</html>
与其用XML从字符串创建DOMSource
,我建议在字符串变量StringReader
上创建StreamSource
https://docs.oracle.com/javase/7/docs/api/javax/XML/transform/stream/StreamSource.html。然后,您可以将该StreamSource传递给Transformer。