当前位置: 首页>>代码示例>>Java>>正文


Java XML.toString方法代码示例

本文整理汇总了Java中org.json.XML.toString方法的典型用法代码示例。如果您正苦于以下问题:Java XML.toString方法的具体用法?Java XML.toString怎么用?Java XML.toString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.json.XML的用法示例。


在下文中一共展示了XML.toString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: shouldHandleIllegalJSONNodeNames

import org.json.XML; //导入方法依赖的package包/类
/**
 * 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);
}
 
开发者ID:stleary,项目名称:JSON-Java-unit-test,代码行数:26,代码来源:XMLTest.java

示例2: jsonToXml

import org.json.XML; //导入方法依赖的package包/类
/**
 * JSON To xml.
 * 
 * @param xmlString the xml string
 * @param rootElement the root element that the xml should have.  
 * By default = gptJsonXml
 * @return the xml. 
 * @throws Exception thrown if error while converting xmlString
 */
public static String jsonToXml(String xmlString, String rootElement) 
  throws Exception {
  try {
    JSONObject jso = new JSONObject(xmlString);
    rootElement = Val.chkStr(rootElement);
    if("".equals(rootElement)) {
      rootElement = "gptJsonXml";
    }
    String xml = XML.toString(jso, "gptJsonXml");
    StreamSource source = new StreamSource(new StringReader(xml));
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(source, result);
    return Val.chkStr(writer.toString());
  } catch (Exception e) {
    throw e;
  }
}
 
开发者ID:usgin,项目名称:usgin-geoportal,代码行数:31,代码来源:XmlIoUtil.java

示例3: getAllZonesForDay

import org.json.XML; //导入方法依赖的package包/类
/**
 * Save incidents for a given day, returns if any data was added by this call.
 */
static final boolean getAllZonesForDay(final LocalDate localDate) throws Exception {
  JSONObject[] stats = new JSONObject[7];
  
  for (int zoneID = 1; zoneID <= 6; zoneID++) {
    stats[zoneID] = getStats(zoneID + "", localDate);
  }
  
  for (int zoneID = 1; zoneID <= 6; zoneID++) {
    JSONArray incidents = stats[zoneID].getJSONArray("incidents");
    if (incidents.length() == 0) {
      Utilities.println("halting getAllZonesForDay on " + localDate + " because zone " + zoneID + " is empty");
      return false;
    }
  }
  
  for (int zoneID = 1; zoneID <= 6; zoneID++) {
    String xmlString = "<stats>" + XML.toString(stats[zoneID]) + "</stats>";
    MongoData.addIncidentsToCollection(getIncidentsFromXMLString(xmlString));
  }
  
  return true;
}
 
开发者ID:rck109d,项目名称:atl-pd-data,代码行数:26,代码来源:Aggregation.java

示例4: xmlFromDOICrossRef

import org.json.XML; //导入方法依赖的package包/类
private Document xmlFromDOICrossRef(String doi) throws Exception {

        //send request to API
        MCRContent crossRefExport = crossRefConnection.getPublicationByDOI(doi);

        //transform JSON-response to XML
        MCRStringContent xml = new MCRStringContent(XML.toString(new JSONObject(crossRefExport.asString()), "content"));

        //transform xml to mods
        MCRXSL2XMLTransformer transformer = new MCRXSL2XMLTransformer("CrossRef2mods.xsl");
        MCRContent mods = transformer.transform(xml);

        return mods.asXML();
    }
 
开发者ID:ETspielberg,项目名称:bibliometrics,代码行数:15,代码来源:AskAllServlet.java

示例5: resolve

import org.json.XML; //导入方法依赖的package包/类
/**
 * Reads document description from the CrossRef API.
 *
 * 
 *   @author Eike Spielberg      
 *
 */

public Source resolve(String href, String base) throws TransformerException {
    String id = href.substring(href.indexOf(":") + 1);
    MCRContent content = null;
    IEEEConnector connection = new IEEEConnector();
    try {
        MCRContent ieeeExport = connection.getPublicationByAuthor(id);
        content = new MCRStringContent(XML.toString(new JSONObject(ieeeExport.asString()), "content"));
        LOGGER.debug("Reading MCRContent with DOI " + id);
        MCRXSL2XMLTransformer transformer = new MCRXSL2XMLTransformer("xsl/IEEE2mods.xsl");
        MCRContent mods = transformer.transform(content);
        connection.close();
        return mods.getSource();
    } catch (Exception e) {
        throw new TransformerException(e);
    }
}
 
开发者ID:ETspielberg,项目名称:bibliometrics,代码行数:25,代码来源:MCRIEEEResolver.java

示例6: resolve

import org.json.XML; //导入方法依赖的package包/类
/**
 * Reads document description from the CrossRef API.
 *
 * 
 *   @author Eike Spielberg      
 *
 */

public Source resolve(String href, String base) throws TransformerException {
    String id = href.substring(href.indexOf(":") + 1);
    MCRContent content = null;
    CrossRefConnector connection = new CrossRefConnector();
    try {
        MCRContent crossRefExport = connection.getPublicationByDOI(id);
        content = new MCRStringContent(XML.toString(new JSONObject(crossRefExport.asString()), "content"));
        LOGGER.debug("Reading MCRContent with DOI " + id);
        MCRXSL2XMLTransformer transformer = new MCRXSL2XMLTransformer("xsl/CrossRef2mods.xsl");
        MCRContent mods = transformer.transform(content);
        connection.close();
        return mods.getSource();
    } catch (Exception e) {
        throw new TransformerException(e);
    }
}
 
开发者ID:ETspielberg,项目名称:bibliometrics,代码行数:25,代码来源:MCRCrossRefResolver.java

示例7: convert

import org.json.XML; //导入方法依赖的package包/类
/**
 * Converts the given JSON string to XML string.
 * class.
 * @param data JSON string
 * @return XML string or an empty string if the conversion fails
 */
@Override
public String convert(String data) {
    String asXML;
    try {
        LOGGER.debug("CONVERTING " + data);
        if (data.startsWith("{")) {
            JSONObject asJson = new JSONObject(data);

            // "array" behaves in a special way, best to disallow it
            if (asJson.has("array")) {
                LOGGER.error("Data violation: Invalid key \"array\"");
                return "<error>Invalid key \"array\"</error>";
            }
            asXML = XML.toString(asJson);
        } else {
            asXML = XML.toString(new JSONArray(data));
        }
        // JSON-LD uses '@' characters in keys and they're not allowed
        // in XML element names. Replace '@' characters with '__at__' in
        // element names.
        asXML = asXML.replaceAll("<(/{0,1})@", "<$1__at__");
        LOGGER.debug("RETURN XML " + asXML);
        return asXML;
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    LOGGER.warn("Converting JSON to XML failed! An empty string is returned.");
    return "";
}
 
开发者ID:vrk-kpa,项目名称:xrd4j,代码行数:36,代码来源:JSONToXMLConverter.java

示例8: jsonToXml

import org.json.XML; //导入方法依赖的package包/类
/**
 * 將 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;
}
 
开发者ID:abola,项目名称:CrawlerPack,代码行数:18,代码来源:CrawlerPack.java

示例9: transform

import org.json.XML; //导入方法依赖的package包/类
@Override
   public String transform(String json) {
String ROOT = null;
       JSONObject jsonObject = null;
    if (json.trim().startsWith("{")) { //$NON-NLS-1$
        jsonObject = new JSONObject(json);
        if (jsonObject.length() > 1) {
            ROOT = "root"; //$NON-NLS-1$
        }
    } else {
        JSONArray jsonArray = new JSONArray(json);
        jsonObject = new JSONObject().put(ELEMENT, jsonArray);
        ROOT = "root";  //$NON-NLS-1$
    }
       return XML.toString(jsonObject, ROOT);
}
 
开发者ID:apiman,项目名称:apiman-plugins,代码行数:17,代码来源:JsonToXmlTransformer.java

示例10: jsonToXml

import org.json.XML; //导入方法依赖的package包/类
@Override
public String jsonToXml(JsonNode json) {
    // remove attachments
    ((ObjectNode) json).remove("_attachments");
    
    // convert to JSONObject and add root element
    JSONObject jsonObject = new JSONObject(json.toString());
    JSONObject rootObject= new JSONObject();
    rootObject.put("document", jsonObject);

    String xml = XML.toString(rootObject);
    return xml;
}
 
开发者ID:martin-kanis,项目名称:relax-dms,代码行数:14,代码来源:DocumentServiceImpl.java

示例11: shouldHandleContentNoArraytoString

import org.json.XML; //导入方法依赖的package包/类
/**
 * 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\":\"\",\""+
        "content\":\">\"},\"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/>&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));
}
 
开发者ID:stleary,项目名称:JSON-Java-unit-test,代码行数:20,代码来源:XMLTest.java

示例12: shouldHandleContentArraytoString

import org.json.XML; //导入方法依赖的package包/类
/**
 * 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\":\"\",\""+
        "content\":[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/>"+
            "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));
}
 
开发者ID:stleary,项目名称:JSON-Java-unit-test,代码行数:22,代码来源:XMLTest.java

示例13: shouldHandleArraytoString

import org.json.XML; //导入方法依赖的package包/类
/**
 * 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));
}
 
开发者ID:stleary,项目名称:JSON-Java-unit-test,代码行数:21,代码来源:XMLTest.java

示例14: shouldHandleNonEmptyArray

import org.json.XML; //导入方法依赖的package包/类
/**
 * Tests that the XML output for arrays is consistent when arrays are not empty.
 */
@Test
public void shouldHandleNonEmptyArray(){
    final JSONObject jo1 = new JSONObject();
    jo1.put("arr",new String[]{"One", "Two", "Three"});
    final JSONObject jo2 = new JSONObject();
    jo2.put("arr",new JSONArray(new String[]{"One", "Two", "Three"}));

    final String expected = "<jo><arr>One</arr><arr>Two</arr><arr>Three</arr></jo>";
    String output1 = XML.toString(jo1,"jo");
    assertEquals("Expected a non empty root tag", expected, output1);
    String output2 = XML.toString(jo2,"jo");
    assertEquals("Expected a non empty root tag", expected, output2);
}
 
开发者ID:stleary,项目名称:JSON-Java-unit-test,代码行数:17,代码来源:XMLTest.java

示例15: shouldHandleMultiArray

import org.json.XML; //导入方法依赖的package包/类
/**
 * 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);
}
 
开发者ID:stleary,项目名称:JSON-Java-unit-test,代码行数:17,代码来源:XMLTest.java


注:本文中的org.json.XML.toString方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。