I have the following code:
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();
}
By using Saxon HE v9.9 it doesn't work getting 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)
Instead of the class XdmAtomicValue I should use another class but I don't know which one.
UPDATED:
The complete code is:
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);
I just need set up json as parameter, no one other file XML is necessary as an entry.
I'm using this solution to transform the JSON file(Martin's answer). By using Saxon HE 9.8 it works fine but using Saxon HE 9.9 it doesn't work.