使用staxon 将xml转换为json时抛出错误:无法装入 translet 类“GregorSamsa”


实现的代码:

public static String xml2jsonBytrans(){
    	InputStream input = StaxonUtils.class.getResourceAsStream("EMR-SD-05-中药处方.xml");
        OutputStream output = System.out;
        /*
         * If we want to insert JSON array boundaries for multiple elements,
         * we need to set the <code>autoArray</code> property.
         * If our XML source was decorated with <code><?xml-multiple?></code>
         * processing instructions, we'd set the <code>multiplePI</code>
         * property instead.
         * With the <code>autoPrimitive</code> property set, element text gets
         * automatically converted to JSON primitives (number, boolean, null).
         */
        JsonXMLConfig config = new JsonXMLConfigBuilder()
            .autoArray(true)
            .autoPrimitive(true)
            .prettyPrint(true)
            .build();
        try {
            /*
             * Create source (XML).
             */
            XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(input);
            Source source = new StAXSource(reader);

            /*
             * Create result (JSON).
             */
            XMLStreamWriter writer = new JsonXMLOutputFactory(config).createXMLStreamWriter(output);
            Result result = new StAXResult(writer);

            /*
             * Copy source to result via "identity transform".
             */
            Templates template = TransformerFactory.newInstance().newTemplates(new StreamSource(StaxonUtils.class.getResourceAsStream("05hospital.xsl")));
            template.newTransformer().transform(source, result);
             return result.toString();
        } catch (Exception e) {
			// TODO: handle exception
        	e.printStackTrace();
        	return "";
		}finally {
            /*
             * As per StAX specification, XMLStreamReader/Writer.close() doesn't close
             * the underlying stream.
             */
            try {
				output.close();
				input.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            
        }
    }

当 05hospital.xsl 文件内容超过64k时就报该错误,小于64k能正常转换,转换的实现类是

com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl  

若添加了了xalan-2.7.2 就会报另一个错误

参考:https://github.com/beckchr/staxon/wiki/Converting-XML-to-JSON

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐