我有以下代码:
public static String converterXml(String json, XsltTransformer transformer) throws SaxonApiException {
DocumentBuilder builder = processor.newDocumentBuilder();
builder.setLineNumbering(true);
builder.setDTDValidation(false);
StringWriter writer = new StringWriter();
Serializer out = processor.newSerializer(writer);
out.setOutputProperty(Serializer.Property.INDENT, "yes");
QName qname = new QName("json-input");
XdmValue value = new XdmAtomicValue(json);
transformer.setParameter(qname, value);
transformer.setDestination(out);
transformer.transform();
return writer.toString();
}
通过使用 Saxon HE v9.9,它无法获取 java.lang.NullPointerException:
Exception in thread "main" java.lang.NullPointerException
at java.util.Objects.requireNonNull(Objects.java:203)
at net.sf.saxon.s9api.AbstractXsltTransformer.applyTemplatesToSource(AbstractXsltTransformer.java:336)
at net.sf.saxon.s9api.XsltTransformer.transform(XsltTransformer.java:338)
我应该使用另一个类而不是XdmAtomicValue类,但我不知道是哪一个。
更新:
完整的代码是:
Processor processor = new Processor(false);
XsltCompiler comp = processor.newXsltCompiler();
XsltTransformer transformer = comp.compile(new StreamSource(new File("/path/json-to-xml.xsl")));
String json = new String(Files.readAllBytes(Paths.get("path-json/invoice.json")));
String xml = converterXml(json, transformer);
我只需要设置 json 作为参数,不需要其他文件 XML 作为条目。
我正在使用这个解决方案来转换 JSON 文件(马丁的回答)。使用 Saxon HE 9.8 可以正常工作,但使用 Saxon HE 9.9 则无法正常工作。
子衿沉夜
相关分类