org.json.XML#toString ( )源码实例Demo

下面列出了org.json.XML#toString ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: JSON-Java-unit-test   文件: XMLTest.java
/**
 * 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);
}
 
源代码2 项目: JSON-Java-unit-test   文件: XMLTest.java
/**
 * 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));
}
 
/**
 * 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/>&gt;"+
            "</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));
}
 
/**
 * 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));
}
 
/**
 * 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));
}
 
/**
 * 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);
}
 
源代码7 项目: JSON-Java-unit-test   文件: XMLTest.java
/**
 * 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("[email protected]", "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><[email protected]>someValue2</[email protected]>";

    assertEquals(expected, result);
}
 
/**
 * 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);
}
 
源代码9 项目: JSON-Java-unit-test   文件: XMLTest.java
/**
 * 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);
}
 
源代码10 项目: Deta_DataBase   文件: VtoV.java
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;	
}
 
源代码11 项目: DETA_BackEnd   文件: VtoV.java
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;	
}
 
源代码12 项目: JSON-Java-unit-test   文件: XMLTest.java
/**
 * 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);
}
 
源代码13 项目: JSON-Java-unit-test   文件: XMLTest.java
/**
 * 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);
}
 
源代码14 项目: JSON-Java-unit-test   文件: XMLTest.java
/**
 * 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);
}
 
源代码15 项目: JSON-Java-unit-test   文件: XMLTest.java
/**
 * 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 &amp; T &gt; e &lt; s &quot; t &apos; 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);
}
 
源代码16 项目: CrawlerPack   文件: CrawlerPack.java
/**
 * 將 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;
}
 
源代码17 项目: JSON-Java-unit-test   文件: XMLTest.java
/**
 * 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());
}
 
源代码18 项目: JSON-Java-unit-test   文件: XMLConfigurationTest.java
/**
 * 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);
}
 
源代码19 项目: JSON-Java-unit-test   文件: XMLConfigurationTest.java
/**
 * 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 &amp; T &gt; e &lt; s &quot; t &apos; 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);
}
 
源代码20 项目: JSON-Java-unit-test   文件: XMLTest.java
/**
 * 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);
}
 
 方法所在类