示例#1
0
 /**
  * @param json the json string
  * @return the string as xml.
  */
 public static String fromJSONtoXML(String json) {
   HierarchicalStreamDriver driver = new JettisonMappedXmlDriver();
   StringReader reader = new StringReader(json);
   HierarchicalStreamReader hsr = driver.createReader(reader);
   StringWriter writer = new StringWriter();
   try {
     new HierarchicalStreamCopier().copy(hsr, new PrettyPrintWriter(writer));
     return writer.toString();
   } finally {
     if (writer != null) {
       try {
         writer.close();
       } catch (IOException e) {
         // ignore
       }
     }
   }
 }
示例#2
0
 /**
  * @param result
  * @return the serialised as xml result of an xpath expression evaluation
  */
 public static String xPathResultToXmlString(Object result) {
   if (result == null) {
     return null;
   }
   try {
     StringWriter sw = new StringWriter();
     Transformer serializer = TransformerFactory.newInstance().newTransformer();
     serializer.setOutputProperty(OutputKeys.INDENT, "yes");
     serializer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml");
     if (result instanceof NodeList) {
       serializer.transform(new DOMSource(((NodeList) result).item(0)), new StreamResult(sw));
     } else if (result instanceof Node) {
       serializer.transform(new DOMSource((Node) result), new StreamResult(sw));
     } else {
       return result.toString();
     }
     return sw.toString();
   } catch (Exception e) {
     throw new RuntimeException("Transformation caused an exception", e);
   }
 }