Java Code Examples for org.json.XML#toString()
The following examples show how to use
org.json.XML#toString() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XMLConfigurationTest.java From JSON-Java-unit-test with Apache License 2.0 | 6 votes |
/** * Tests that the XML output for arrays is consistent when arrays are not empty and contain internal arrays. */ @Test public void shouldHandleMultiArray(){ final JSONObject jo1 = new JSONObject(); jo1.put("arr",new Object[]{"One", new String[]{"Two", "Three"}, "Four"}); final JSONObject jo2 = new JSONObject(); jo2.put("arr",new JSONArray(new Object[]{"One", new JSONArray(new String[]{"Two", "Three"}), "Four"})); final String expected = "<jo><arr>One</arr><arr><array>Two</array><array>Three</array></arr><arr>Four</arr></jo>"; String output1 = XML.toString(jo1, "jo", XMLParserConfiguration.KEEP_STRINGS); assertEquals("Expected a matching array", expected, output1); String output2 = XML.toString(jo2, "jo", XMLParserConfiguration.KEEP_STRINGS); assertEquals("Expected a matching array", expected, output2); }
Example 2
Source File: XMLTest.java From JSON-Java-unit-test with Apache License 2.0 | 6 votes |
/** * Possible bug: * Illegal node-names must be converted to legal XML-node-names. * The given example shows 2 nodes which are valid for JSON, but not for XML. * Therefore illegal arguments should be converted to e.g. an underscore (_). */ @Test public void shouldHandleIllegalJSONNodeNames() { JSONObject inputJSON = new JSONObject(); inputJSON.append("123IllegalNode", "someValue1"); inputJSON.append("Illegal@node", "someValue2"); String result = XML.toString(inputJSON); /* * This is invalid XML. Names should not begin with digits or contain * certain values, including '@'. One possible solution is to replace * illegal chars with '_', in which case the expected output would be: * <___IllegalNode>someValue1</___IllegalNode><Illegal_node>someValue2</Illegal_node> */ String expected = "<123IllegalNode>someValue1</123IllegalNode><Illegal@node>someValue2</Illegal@node>"; assertEquals(expected, result); }
Example 3
Source File: XMLConfigurationTest.java From JSON-Java-unit-test with Apache License 2.0 | 6 votes |
/** * test passes when using the new method toJsonArray. */ @Test public void testToJsonXML() { final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>"; final String expectedJsonString = "{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",\"1\",\"00\",\"0\"],\"title\":\"True\"}}"; final JSONObject json = XML.toJSONObject(originalXml, new XMLParserConfiguration(true)); assertEquals(expectedJsonString, json.toString()); final String reverseXml = XML.toString(json); // this reversal isn't exactly the same. use JSONML for an exact reversal final String expectedReverseXml = "<root><item><id>01</id></item><id>01</id><id>1</id><id>00</id><id>0</id><title>True</title></root>"; assertEquals(expectedReverseXml, reverseXml); }
Example 4
Source File: XMLTest.java From JSON-Java-unit-test with Apache License 2.0 | 6 votes |
/** * JSONObject with NULL value, to XML.toString() */ @Test public void shouldHandleNullNodeValue() { JSONObject inputJSON = new JSONObject(); inputJSON.put("nullValue", JSONObject.NULL); // This is a possible preferred result // String expectedXML = "<nullValue/>"; /** * This is the current behavior. JSONObject.NULL is emitted as * the string, "null". */ String actualXML = "<nullValue>null</nullValue>"; String resultXML = XML.toString(inputJSON); assertEquals(actualXML, resultXML); }
Example 5
Source File: XMLConfigurationTest.java From JSON-Java-unit-test with Apache License 2.0 | 6 votes |
/** * Converting a JSON doc containing a named array to JSONObject, then * XML.toString() should result in valid XML. */ @Test public void shouldHandleArraytoString() { String expectedStr = "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\","+ "\"something\":[1, 2, 3]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; JSONObject expectedJsonObject = new JSONObject(expectedStr); String finalStr = XML.toString(expectedJsonObject, null, XMLParserConfiguration.KEEP_STRINGS); String expectedFinalStr = "<addresses><address><name/><nocontent/>"+ "<something>1</something><something>2</something><something>3</something>"+ "</address><xsi:noNamespaceSchemaLocation>test.xsd</xsi:noName"+ "spaceSchemaLocation><xmlns:xsi>http://www.w3.org/2001/XMLSche"+ "ma-instance</xmlns:xsi></addresses>"; assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+ finalStr+"]", expectedFinalStr.equals(finalStr)); }
Example 6
Source File: XMLConfigurationTest.java From JSON-Java-unit-test with Apache License 2.0 | 6 votes |
/** * Converting a JSON doc containing a 'content' array to JSONObject, then * XML.toString() should result in valid XML. * TODO: This is probably an error in how the 'content' keyword is used. */ @Test public void shouldHandleContentArraytoString() { String expectedStr = "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+ "altContent\":[1, 2, 3]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; JSONObject expectedJsonObject = new JSONObject(expectedStr); XMLParserConfiguration config = new XMLParserConfiguration("altContent"); String finalStr = XML.toString(expectedJsonObject, null, config); String expectedFinalStr = "<addresses><address><name/><nocontent/>"+ "1\n2\n3"+ "</address><xsi:noNamespaceSchemaLocation>test.xsd</xsi:noName"+ "spaceSchemaLocation><xmlns:xsi>http://www.w3.org/2001/XMLSche"+ "ma-instance</xmlns:xsi></addresses>"; assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+ finalStr+"]", expectedFinalStr.equals(finalStr)); }
Example 7
Source File: XMLConfigurationTest.java From JSON-Java-unit-test with Apache License 2.0 | 6 votes |
/** * Converting a JSON doc containing '>' content to JSONObject, then * XML.toString() should result in valid XML. */ @Test public void shouldHandleContentNoArraytoString() { String expectedStr = "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+ "altContent\":\">\"},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; JSONObject expectedJsonObject = new JSONObject(expectedStr); XMLParserConfiguration config = new XMLParserConfiguration("altContent"); String finalStr = XML.toString(expectedJsonObject, null, config); String expectedFinalStr = "<addresses><address><name/><nocontent/>>"+ "</address><xsi:noNamespaceSchemaLocation>test.xsd</xsi:noName"+ "spaceSchemaLocation><xmlns:xsi>http://www.w3.org/2001/XMLSche"+ "ma-instance</xmlns:xsi></addresses>"; assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+ finalStr+"]", expectedFinalStr.equals(finalStr)); }
Example 8
Source File: XMLTest.java From JSON-Java-unit-test with Apache License 2.0 | 6 votes |
/** * Converting a JSON doc containing a named array to JSONObject, then * XML.toString() should result in valid XML. */ @Test public void shouldHandleArraytoString() { String expectedStr = "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\","+ "\"something\":[1, 2, 3]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+ "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}"; JSONObject expectedJsonObject = new JSONObject(expectedStr); String finalStr = XML.toString(expectedJsonObject); String expectedFinalStr = "<addresses><address><name/><nocontent/>"+ "<something>1</something><something>2</something><something>3</something>"+ "</address><xsi:noNamespaceSchemaLocation>test.xsd</xsi:noName"+ "spaceSchemaLocation><xmlns:xsi>http://www.w3.org/2001/XMLSche"+ "ma-instance</xmlns:xsi></addresses>"; assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+ finalStr+"]", expectedFinalStr.equals(finalStr)); }
Example 9
Source File: XMLConfigurationTest.java From JSON-Java-unit-test with Apache License 2.0 | 5 votes |
/** * Null JSONObject in XML.toString() */ @Test public void shouldHandleNullJSONXML() { JSONObject jsonObject= null; String actualXml = XML.toString(jsonObject, null, XMLParserConfiguration.KEEP_STRINGS); assertEquals("generated XML does not equal expected XML","\"null\"",actualXml); }
Example 10
Source File: XMLTest.java From JSON-Java-unit-test with Apache License 2.0 | 5 votes |
/** * test passes when using the new method toJsonArray. */ @Test public void testToJsonXML() { final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>"; final String expectedJsonString = "{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",\"1\",\"00\",\"0\"],\"title\":\"True\"}}"; final JSONObject json = XML.toJSONObject(originalXml,true); assertEquals(expectedJsonString, json.toString()); final String reverseXml = XML.toString(json); // this reversal isn't exactly the same. use JSONML for an exact reversal final String expectedReverseXml = "<root><item><id>01</id></item><id>01</id><id>1</id><id>00</id><id>0</id><title>True</title></root>"; assertEquals(expectedReverseXml, reverseXml); }
Example 11
Source File: XMLConfigurationTest.java From JSON-Java-unit-test with Apache License 2.0 | 5 votes |
/** * Valid XML to XML.toString() */ @Test public void shouldHandleToString() { String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+ "<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+ " xsi:noNamespaceSchemaLocation='test.xsd'>\n"+ " <address>\n"+ " <name>[CDATA[Joe & T > e < s " t ' er]]</name>\n"+ " <street>Baker street 5</street>\n"+ " <ArrayOfNum>1, 2, 3, 4.1, 5.2</ArrayOfNum>\n"+ " </address>\n"+ "</addresses>"; String expectedStr = "{\"addresses\":{\"address\":{\"street\":\"Baker street 5\","+ "\"name\":\"[CDATA[Joe & T > e < s \\\" t \\\' er]]\","+ "\"ArrayOfNum\":\"1, 2, 3, 4.1, 5.2\"\n"+ "},\"xsi:noNamespaceSchemaLocation\":"+ "\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+ "XMLSchema-instance\"}}"; JSONObject jsonObject = XML.toJSONObject(xmlStr, XMLParserConfiguration.KEEP_STRINGS); String xmlToStr = XML.toString(jsonObject, null, XMLParserConfiguration.KEEP_STRINGS); JSONObject finalJsonObject = XML.toJSONObject(xmlToStr, XMLParserConfiguration.KEEP_STRINGS); JSONObject expectedJsonObject = new JSONObject(expectedStr); Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); }
Example 12
Source File: XMLTest.java From JSON-Java-unit-test with Apache License 2.0 | 5 votes |
/** * JSON string cannot be reverted to original xml. */ @Test public void testToJSONArray_reversibility() { final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>"; final String revertedXml = XML.toString(XML.toJSONObject(originalXml, false)); assertNotEquals(revertedXml, originalXml); }
Example 13
Source File: XMLTest.java From JSON-Java-unit-test with Apache License 2.0 | 5 votes |
/** * Empty JSONObject in XML.toString() */ @Test public void shouldHandleEmptyJSONXML() { JSONObject jsonObject= new JSONObject(); String xmlStr = XML.toString(jsonObject); assertTrue("xml string should be empty", xmlStr.isEmpty()); }
Example 14
Source File: CrawlerPack.java From CrawlerPack with Apache License 2.0 | 5 votes |
/** * 將 json 轉為 XML * * @param json a json format string. * @return XML format string */ public String jsonToXml(String json){ String xml = ""; // 處理直接以陣列開頭的JSON,並指定給予 row 的 tag if ( "[".equals( json.substring(0,1) ) ){ xml = XML.toString(new JSONArray(json), "row"); }else{ xml = XML.toString(new JSONObject(json)); } return xml; }
Example 15
Source File: XMLTest.java From JSON-Java-unit-test with Apache License 2.0 | 5 votes |
/** * Valid XML to XML.toString() */ @Test public void shouldHandleToString() { String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+ "<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+ " xsi:noNamespaceSchemaLocation='test.xsd'>\n"+ " <address>\n"+ " <name>[CDATA[Joe & T > e < s " t ' er]]</name>\n"+ " <street>Baker street 5</street>\n"+ " <ArrayOfNum>1, 2, 3, 4.1, 5.2</ArrayOfNum>\n"+ " </address>\n"+ "</addresses>"; String expectedStr = "{\"addresses\":{\"address\":{\"street\":\"Baker street 5\","+ "\"name\":\"[CDATA[Joe & T > e < s \\\" t \\\' er]]\","+ "\"ArrayOfNum\":\"1, 2, 3, 4.1, 5.2\"\n"+ "},\"xsi:noNamespaceSchemaLocation\":"+ "\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+ "XMLSchema-instance\"}}"; JSONObject jsonObject = XML.toJSONObject(xmlStr); String xmlToStr = XML.toString(jsonObject); JSONObject finalJsonObject = XML.toJSONObject(xmlToStr); JSONObject expectedJsonObject = new JSONObject(expectedStr); Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); }
Example 16
Source File: XMLTest.java From JSON-Java-unit-test with Apache License 2.0 | 5 votes |
/** * Null JSONObject in XML.toString() */ @Test public void shouldHandleNullJSONXML() { JSONObject jsonObject= null; String actualXml=XML.toString(jsonObject); assertEquals("generated XML does not equal expected XML","\"null\"",actualXml); }
Example 17
Source File: XMLTest.java From JSON-Java-unit-test with Apache License 2.0 | 5 votes |
/** * Tests that the XML output for empty arrays is consistent. */ @Test public void shouldHandleEmptyArray(){ final JSONObject jo1 = new JSONObject(); jo1.put("array",new Object[]{}); final JSONObject jo2 = new JSONObject(); jo2.put("array",new JSONArray()); final String expected = "<jo></jo>"; String output1 = XML.toString(jo1,"jo"); assertEquals("Expected an empty root tag", expected, output1); String output2 = XML.toString(jo2,"jo"); assertEquals("Expected an empty root tag", expected, output2); }
Example 18
Source File: XMLTest.java From JSON-Java-unit-test with Apache License 2.0 | 5 votes |
/** * Tests that the XML output for arrays is consistent when arrays are not empty and contain internal arrays. */ @Test public void shouldHandleMultiArray(){ final JSONObject jo1 = new JSONObject(); jo1.put("arr",new Object[]{"One", new String[]{"Two", "Three"}, "Four"}); final JSONObject jo2 = new JSONObject(); jo2.put("arr",new JSONArray(new Object[]{"One", new JSONArray(new String[]{"Two", "Three"}), "Four"})); final String expected = "<jo><arr>One</arr><arr><array>Two</array><array>Three</array></arr><arr>Four</arr></jo>"; String output1 = XML.toString(jo1,"jo"); assertEquals("Expected a matching array", expected, output1); String output2 = XML.toString(jo2,"jo"); assertEquals("Expected a matching array", expected, output2); }
Example 19
Source File: VtoV.java From DETA_BackEnd with Apache License 2.0 | 5 votes |
public String MapToXml(Map<String, Object> response){ Gson gson = new Gson(); String json = gson.toJson(response); JSONObject jsonObj = new JSONObject(json); String xml = XML.toString(jsonObj); return xml; }
Example 20
Source File: VtoV.java From Deta_DataBase with Apache License 2.0 | 5 votes |
public static String MapToXml(Map<String, Object> response){ //下面这行代码独立作者 罗瑶光 2016年已转让给走四方。本系统没有用到这函数。 Gson gson = new Gson(); String json = gson.toJson(response); JSONObject jsonObj = new JSONObject(json); String xml = XML.toString(jsonObj); return xml; }