@Test public void testJson_To_XML_Byte_Array_Simple_Object() throws IOException { try(FileInputStream fis = new FileInputStream("src/test/resources/simple-object.json")){ byte[] result = processor.convertJsonToXMLBytes(fis, null, null, "NS", null, "UTF-8", "TEST"); assertThat(new String(result, StandardCharsets.UTF_8), equalTo("<?xml version=\"1.0\" encoding=\"UTF-8\"?><TEST xmlns=\"NS\"><education>HU</education><education>MTU</education><name><last_name>Zhan</last_name><first_name>Nicholas</first_name></name><tel>12345678901</tel><position>Developer</position><age>22</age></TEST>")); } }
protected byte[] convertJsonToXMLBytes(InputStream jsonInputStream, String jsonPathExpression, String attributeMark, String nameSpace, String contentKeyName, String encoding, String elementName) throws IOException { try(InputStreamReader isr = new InputStreamReader(jsonInputStream, StandardCharsets.UTF_8); BufferedReader br = new BufferedReader(isr)){ XMLConverter xmlInstance = new XMLConverter(); xmlInstance.setNameSpace(nameSpace); xmlInstance.setAttributeMark(attributeMark); if (contentKeyName != null) { xmlInstance.setDataTagName(contentKeyName); } else { xmlInstance.setDataTagName(""); } if (jsonPathExpression == null) { Object json =new JSONTokener(br).nextValue(); if(json instanceof JSONObject){ return xmlInstance.toBytes_p(json, elementName, encoding); } else if(json instanceof JSONArray){ return xmlInstance.toBytes_p(json, "list", encoding); } JSONObject jsonObject = new JSONObject(jsonInputStream); return xmlInstance.toBytes_p(jsonObject, elementName, encoding); } else { Object items = JsonPath.read(jsonInputStream, jsonPathExpression); Map<String, Object> map = new HashMap<>(); map.put(elementName, items); JSONObject jsonObject = new JSONObject(map); if ((items instanceof net.minidev.json.JSONArray || items.getClass().isArray())) { if (((List) items).size() == 0) { map.put(elementName, ""); jsonObject = new JSONObject(map); } return xmlInstance.toBytes_p(jsonObject, "list", encoding); } else { return xmlInstance.toBytes_p(jsonObject, null, encoding); } } } }
ConvertJSONToXML extends AbstractProcessor { protected byte[] convertJsonToXMLBytes(InputStream jsonInputStream, String jsonPathExpression, String attributeMark, String nameSpace, String contentKeyName, String encoding, String elementName) throws IOException { try(InputStreamReader isr = new InputStreamReader(jsonInputStream, StandardCharsets.UTF_8); BufferedReader br = new BufferedReader(isr)){ XMLConverter xmlInstance = new XMLConverter(); xmlInstance.setNameSpace(nameSpace); xmlInstance.setAttributeMark(attributeMark); if (contentKeyName != null) { xmlInstance.setDataTagName(contentKeyName); } else { xmlInstance.setDataTagName(""); } if (jsonPathExpression == null) { Object json =new JSONTokener(br).nextValue(); if(json instanceof JSONObject){ return xmlInstance.toBytes_p(json, elementName, encoding); } else if(json instanceof JSONArray){ return xmlInstance.toBytes_p(json, "list", encoding); } JSONObject jsonObject = new JSONObject(jsonInputStream); return xmlInstance.toBytes_p(jsonObject, elementName, encoding); } else { Object items = JsonPath.read(jsonInputStream, jsonPathExpression); Map<String, Object> map = new HashMap<>(); map.put(elementName, items); JSONObject jsonObject = new JSONObject(map); if ((items instanceof net.minidev.json.JSONArray || items.getClass().isArray())) { if (((List) items).size() == 0) { map.put(elementName, ""); jsonObject = new JSONObject(map); } return xmlInstance.toBytes_p(jsonObject, "list", encoding); } else { return xmlInstance.toBytes_p(jsonObject, null, encoding); } } } } }
ConvertJSONToXML extends AbstractProcessor { protected byte[] convertJsonToXMLBytes(InputStream jsonInputStream, String jsonPathExpression, String attributeMark, String nameSpace, String contentKeyName, String encoding, String elementName) throws IOException { try(InputStreamReader isr = new InputStreamReader(jsonInputStream, StandardCharsets.UTF_8); BufferedReader br = new BufferedReader(isr)){ XMLConverter xmlInstance = new XMLConverter(); xmlInstance.setNameSpace(nameSpace); xmlInstance.setAttributeMark(attributeMark); if (contentKeyName != null) { xmlInstance.setDataTagName(contentKeyName); } else { xmlInstance.setDataTagName(""); } if (jsonPathExpression == null) { Object json =new JSONTokener(br).nextValue(); if(json instanceof JSONObject){ return xmlInstance.toBytes_p(json, elementName, encoding); } else if(json instanceof JSONArray){ return xmlInstance.toBytes_p(json, "list", encoding); } JSONObject jsonObject = new JSONObject(jsonInputStream); return xmlInstance.toBytes_p(jsonObject, elementName, encoding); } else { Object items = JsonPath.read(jsonInputStream, jsonPathExpression); Map<String, Object> map = new HashMap<>(); map.put(elementName, items); JSONObject jsonObject = new JSONObject(map); if ((items instanceof net.minidev.json.JSONArray || items.getClass().isArray())) { if (((List) items).size() == 0) { map.put(elementName, ""); jsonObject = new JSONObject(map); } return xmlInstance.toBytes_p(jsonObject, "list", encoding); } else { return xmlInstance.toBytes_p(jsonObject, null, encoding); } } } }  }
ConvertJSONToXML extends AbstractProcessor { protected byte[] convertJsonToXMLBytes(InputStream jsonInputStream, String jsonPathExpression, String attributeMark, String nameSpace, String contentKeyName, String encoding, String elementName) throws IOException { try(InputStreamReader isr = new InputStreamReader(jsonInputStream, StandardCharsets.UTF_8); BufferedReader br = new BufferedReader(isr)){ XMLConverter xmlInstance = new XMLConverter(); xmlInstance.setNameSpace(nameSpace); xmlInstance.setAttributeMark(attributeMark); if (contentKeyName != null) { xmlInstance.setDataTagName(contentKeyName); } else { xmlInstance.setDataTagName(""); } if (jsonPathExpression == null) { Object json =new JSONTokener(br).nextValue(); if(json instanceof JSONObject){ return xmlInstance.toBytes_p(json, elementName, encoding); } else if(json instanceof JSONArray){ return xmlInstance.toBytes_p(json, "list", encoding); } JSONObject jsonObject = new JSONObject(jsonInputStream); return xmlInstance.toBytes_p(jsonObject, elementName, encoding); } else { Object items = JsonPath.read(jsonInputStream, jsonPathExpression); Map<String, Object> map = new HashMap<>(); map.put(elementName, items); JSONObject jsonObject = new JSONObject(map); if ((items instanceof net.minidev.json.JSONArray || items.getClass().isArray())) { if (((List) items).size() == 0) { map.put(elementName, ""); jsonObject = new JSONObject(map); } return xmlInstance.toBytes_p(jsonObject, "list", encoding); } else { return xmlInstance.toBytes_p(jsonObject, null, encoding); } } } }  @Override Set<Relationship> getRelationships(); @OnScheduled void onScheduled(ProcessContext context); @Override void onTrigger(ProcessContext context, ProcessSession session); }
ConvertJSONToXML extends AbstractProcessor { protected byte[] convertJsonToXMLBytes(InputStream jsonInputStream, String jsonPathExpression, String attributeMark, String nameSpace, String contentKeyName, String encoding, String elementName) throws IOException { try(InputStreamReader isr = new InputStreamReader(jsonInputStream, StandardCharsets.UTF_8); BufferedReader br = new BufferedReader(isr)){ XMLConverter xmlInstance = new XMLConverter(); xmlInstance.setNameSpace(nameSpace); xmlInstance.setAttributeMark(attributeMark); if (contentKeyName != null) { xmlInstance.setDataTagName(contentKeyName); } else { xmlInstance.setDataTagName(""); } if (jsonPathExpression == null) { Object json =new JSONTokener(br).nextValue(); if(json instanceof JSONObject){ return xmlInstance.toBytes_p(json, elementName, encoding); } else if(json instanceof JSONArray){ return xmlInstance.toBytes_p(json, "list", encoding); } JSONObject jsonObject = new JSONObject(jsonInputStream); return xmlInstance.toBytes_p(jsonObject, elementName, encoding); } else { Object items = JsonPath.read(jsonInputStream, jsonPathExpression); Map<String, Object> map = new HashMap<>(); map.put(elementName, items); JSONObject jsonObject = new JSONObject(map); if ((items instanceof net.minidev.json.JSONArray || items.getClass().isArray())) { if (((List) items).size() == 0) { map.put(elementName, ""); jsonObject = new JSONObject(map); } return xmlInstance.toBytes_p(jsonObject, "list", encoding); } else { return xmlInstance.toBytes_p(jsonObject, null, encoding); } } } }  @Override Set<Relationship> getRelationships(); @OnScheduled void onScheduled(ProcessContext context); @Override void onTrigger(ProcessContext context, ProcessSession session); static final String XML_ATTRIBUTE_NAME; static final String DESTINATION_ATTRIBUTE; static final String DESTINATION_CONTENT; static final String APPLICATION_XML; static final PropertyDescriptor DESTINATION; static final Relationship REL_SUCCESS; static final Relationship REL_FAILURE; }