Example usage for javax.xml.transform.stream StreamResult toString

List of usage examples for javax.xml.transform.stream StreamResult toString

Introduction

In this page you can find the example usage for javax.xml.transform.stream StreamResult toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:ORG.oclc.os.SRW.xml2jsonTransformer.java

@Override
public void transform(Source xmlSource, Result outputTarget) throws TransformerException {
    StreamSource source = (StreamSource) xmlSource;
    BufferedReader br = new BufferedReader(source.getReader());
    StringBuilder sb = new StringBuilder();
    String line;//from w  ww  . j  a  va2  s  .co  m
    try {
        while ((line = br.readLine()) != null)
            sb.append(line);
    } catch (IOException ex) {
        throw new TransformerException(ex);
    }
    String xmlRecord = sb.toString();
    if (log.isDebugEnabled())
        log.debug("xmlSource: " + xmlRecord);
    XMLSerializer xmlSerializer = new XMLSerializer();
    try {
        JSON json = xmlSerializer.read(xmlRecord);
        StreamResult sr = (StreamResult) outputTarget;
        json.write(sr.getWriter());
        if (log.isDebugEnabled())
            log.debug("convertedJSON: " + sr.toString());
    } catch (Exception e) {
        log.error("Unable to transform to JSON: " + xmlRecord);
        throw new TransformerException(e);
    }
}