Exemplo n.º 1
0
  @Override
  public Scenario parse(InputStream inputStream)
      throws IOException, SAXException, ParserConfigurationException {

    OutputStream output = new ByteArrayOutputStream();
    JsonXMLConfig config = new JsonXMLConfigBuilder().autoArray(true).prettyPrint(false).build();

    try {

      /* Create source (XML). */
      XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(inputStream);
      // XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
      // Source source = new StAXSource(reader);

      /* Create result (JSON). */
      XMLEventWriter writer = new JsonXMLOutputFactory(config).createXMLEventWriter(output);
      // XMLStreamWriter writer = new JsonXMLOutputFactory(config).createXMLStreamWriter(output);
      // Result result = new StAXResult(writer);

      /*
       * Copy events from reader to writer.
       */
      writer.add(reader);

      /* Copy source to result via "identity transform". */
      // TransformerFactory.newInstance().newTransformer().transform(source, result);
    } catch (XMLStreamException e) {
      e.printStackTrace();
    } finally {

      /* As per StAX specification, XMLStreamReader/Writer.close()
      doesn't close
      the underlying stream. */
      output.close();
      inputStream.close();
    }

    /*
    try {
        json = xmlToJson(inputStream);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    String jsonString = json.toString();

    System.out.println(jsonString);

    GenericDocument genericDocument = new GenericDocument();
    genericDocument.setJson(jsonString);
    */

    GenericDocument genericDocument = new GenericDocument();
    String json = output.toString();

    genericDocument.setJson(json);

    return genericDocument;
  }
Exemplo n.º 2
0
  public static String httpget(String url) {
    try {
      StringBuffer sb = new StringBuffer();
      URL href = new URL(url);
      HttpURLConnection.setFollowRedirects(true);
      HttpURLConnection hc = (HttpURLConnection) href.openConnection();
      String ua = "Mozilla/4.0 (compatible; MSIE 6.0; WINDOWS; .NET CLR 1.1.4322)";
      hc.setRequestProperty("user-agent", ua);
      hc.setRequestMethod("GET");
      hc.connect();

      InputStream is = hc.getInputStream();
      int i;
      while ((i = is.read()) != -1) {
        char c = (char) i;
        sb.append(c);
      }
      is.close();
      hc.disconnect();
      return new String(sb);
    } catch (Exception e) {
      return null;
    }
  }