Example usage for javax.xml.stream XMLOutputFactory createXMLEventWriter

List of usage examples for javax.xml.stream XMLOutputFactory createXMLEventWriter

Introduction

In this page you can find the example usage for javax.xml.stream XMLOutputFactory createXMLEventWriter.

Prototype

public abstract XMLEventWriter createXMLEventWriter(java.io.Writer stream) throws XMLStreamException;

Source Link

Document

Create a new XMLEventWriter that writes to a writer

Usage

From source file:org.talend.repository.json.util.ConvertJSONString.java

public static void main(String[] args) {
    ConvertJSONString convertJSON = new ConvertJSONString();
    de.odysseus.staxon.json.JsonXMLConfig jsonConfig = new de.odysseus.staxon.json.JsonXMLConfigBuilder()
            .multiplePI(false).build();/*from  w w  w  .j a v  a2s  .c om*/
    de.odysseus.staxon.json.JsonXMLInputFactory jsonXMLInputFactory = new de.odysseus.staxon.json.JsonXMLInputFactory(
            jsonConfig);
    javax.xml.stream.XMLOutputFactory xmlOutputFactory = javax.xml.stream.XMLOutputFactory.newInstance();

    // org.dom4j.io.SAXReader reader_tExtractJSONFields_1 = new org.dom4j.io.SAXReader();
    // org.dom4j.Document doc_tExtractJSONFields_1 = null;
    // java.util.HashMap xmlNameSpaceMap_tExtractJSONFields_1 = new java.util.HashMap<String, String>();
    // org.dom4j.XPath x_tExtractJSONFields_1 = null;
    // java.util.List<org.dom4j.tree.AbstractNode> nodeList_tExtractJSONFields_1 = null;
    // boolean isStructError = true;
    java.io.ByteArrayOutputStream outStream = new java.io.ByteArrayOutputStream();
    java.io.ByteArrayInputStream inStream = null;
    File file = new File("E:/builds/source_file.json");
    try {
        FileInputStream input = new FileInputStream(file);
        String jsonStr = IOUtils.toString(input);

        // String loopQuery_tExtractJSONFields_1 = "/rcp.authorized_plug-ins";
        // String oraginalJsonStr_tExtractJSONFields_1 = jsonStr_tExtractJSONFields_1;
        convertJSON.setJsonString(jsonStr);
        // cjs_tExtractJSONFields_1.setLoopString(loopQuery_tExtractJSONFields_1);

        convertJSON.generate();
        jsonStr = convertJSON.getJsonString4XML();
        // loopQuery_tExtractJSONFields_1 = cjs_tExtractJSONFields_1.getLoopString4XML();
        inStream = new ByteArrayInputStream(jsonStr.getBytes());
        javax.xml.stream.XMLEventReader xmlEventReader = jsonXMLInputFactory.createXMLEventReader(inStream);
        javax.xml.stream.XMLEventWriter xmLEventWriter = xmlOutputFactory.createXMLEventWriter(outStream);
        xmLEventWriter.add(xmlEventReader);
        // convert json string to xml
        String xmlStr = outStream.toString();
        // System.out.println(xmlStr_tExtractJSONFields_1);

        FileWriter writer = new FileWriter("D:/CLOUD1.xml");
        writer.write(xmlStr);
        writer.flush();
        writer.close();
        // xmlStr_tExtractJSONFields_1 =
        // "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><rcp.authorized_plug-ins><s1>4</s1><upk>false</upk><psn>org.eclipse.jface.databinging.n1_de</psn><v>3.6.0.v20100814043401</v><pa>MDM1,TIS,DI</pa><sd>false</sd></rcp.authorized_plug-ins><rcp.authorized_plug-ins><s1>1</s1><upk>false</upk><psn>org.eclipse.jface.databinging.n1_de</psn><v>3.6.0.v20100814043401</v><pa>TDQ,Test</pa><sd>false</sd></rcp.authorized_plug-ins></root>";
        xmLEventWriter.close();
        xmlEventReader.close();

        // doc_tExtractJSONFields_1 = reader_tExtractJSONFields_1.read(new
        // java.io.StringReader(xmlStr_tExtractJSONFields_1));
        // x_tExtractJSONFields_1 = doc_tExtractJSONFields_1.createXPath(loopQuery_tExtractJSONFields_1);
        // x_tExtractJSONFields_1.setNamespaceURIs(xmlNameSpaceMap_tExtractJSONFields_1);
        // nodeList_tExtractJSONFields_1 = (java.util.List<org.dom4j.tree.AbstractNode>) x_tExtractJSONFields_1
        // .selectNodes(doc_tExtractJSONFields_1);
        // isStructError = false;
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (java.lang.Exception ex_tExtractJSONFields_1) {
        ex_tExtractJSONFields_1.printStackTrace();
    } finally {
        try {
            outStream.close();
            if (inStream != null) {
                inStream.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

From source file:org.talend.repository.json.util.JSONUtil.java

public static String changeJsonToXml(String jsonPath) {
    Project project = ProjectManager.getInstance().getCurrentProject();
    IProject fsProject = null;//www .jav a2  s  .  c  o  m
    try {
        fsProject = ResourceUtils.getProject(project);
    } catch (PersistenceException e2) {
        ExceptionHandler.process(e2);
    }
    if (fsProject == null) {
        return jsonPath;
    }
    String temPath = fsProject.getLocationURI().getPath() + File.separator + "temp" + File.separator
            + "jsonwizard" + File.separator;

    ConvertJSONString convertJSON = new ConvertJSONString();
    de.odysseus.staxon.json.JsonXMLConfig jsonConfig = new de.odysseus.staxon.json.JsonXMLConfigBuilder()
            .multiplePI(false).build();
    de.odysseus.staxon.json.JsonXMLInputFactory jsonXMLInputFactory = new de.odysseus.staxon.json.JsonXMLInputFactory(
            jsonConfig);
    javax.xml.stream.XMLOutputFactory xmlOutputFactory = javax.xml.stream.XMLOutputFactory.newInstance();

    java.io.ByteArrayOutputStream outStream = new java.io.ByteArrayOutputStream();
    InputStream inStream = null;
    File file = new File(jsonPath);

    // String filename = file.getName().replaceAll("\\.", "_");
    // filename = "tempTest";
    boolean isFromUrl = false;
    boolean illegalURL = false;
    InputStream input = null;

    if (file.exists()) {
        if (file.isDirectory()) {
            return "";
        }
        try {
            input = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            ExceptionHandler.process(e);
        }
    } else {
        isFromUrl = true;
        try {
            input = new URL(jsonPath).openStream();
        } catch (MalformedURLException e) {
            illegalURL = true;
        } catch (IOException e) {
            illegalURL = true;
        }
        if (illegalURL) {
            return "";
        }
    }

    try {
        String jsonStr = IOUtils.toString(input);

        convertJSON.setJsonString(jsonStr);

        convertJSON.generate();
        jsonStr = convertJSON.getJsonString4XML();
        inStream = new ByteArrayInputStream(jsonStr.getBytes());
        javax.xml.stream.XMLEventReader xmlEventReader = jsonXMLInputFactory.createXMLEventReader(inStream);
        javax.xml.stream.XMLEventWriter xmLEventWriter = xmlOutputFactory.createXMLEventWriter(outStream);
        xmLEventWriter.add(xmlEventReader);
        String xmlStr = outStream.toString();

        File xmlFolder = new File(temPath);
        if (!xmlFolder.exists()) {
            xmlFolder.mkdirs();
        }
        temPath = temPath + TMP_JSON_FILE;
        FileWriter writer = new FileWriter(temPath);
        writer.write(xmlStr);
        writer.flush();
        writer.close();

        xmLEventWriter.close();
        xmlEventReader.close();
        if (isFromUrl) {
            tempJSONXsdPath = temPath;
        }
    } catch (java.lang.Exception e) {
        ExceptionHandler.process(e);
    } finally {
        try {
            outStream.close();
            if (inStream != null) {
                inStream.close();
            }
        } catch (IOException e) {
            ExceptionHandler.process(e);
        }

    }
    return temPath;
}

From source file:ru.codeinside.gws.crypto.cryptopro.CryptoProvider.java

private String saxFilter(Node node) {
    try {/*from   ww w  . ja  va2  s  .c o  m*/
        final Transformer transformer = TransformerFactory.newInstance().newTransformer();
        final StringWriter w1 = new StringWriter();
        transformer.transform(new DOMSource(node), new StreamResult(w1));
        XMLInputFactory xif = XMLInputFactory.newInstance();
        XMLEventReader eventReader = xif
                .createXMLEventReader(new StreamSource(new StringReader(w1.toString())));
        XMLEventReader filteredReader = xif.createFilteredReader(eventReader, new EventFilter() {
            @Override
            public boolean accept(XMLEvent event) {
                int type = event.getEventType();
                if (type == XMLStreamConstants.START_DOCUMENT || type == XMLStreamConstants.END_DOCUMENT) {
                    return false;
                }
                if (event.isStartElement()) {
                    StartElement startElement = (StartElement) event;
                    QName name = startElement.getName();
                    if ("".equals(name.getNamespaceURI()) && "root".equals(name.getLocalPart())) {
                        return false;
                    }
                }
                if (event.isEndElement()) {
                    EndElement endElement = (EndElement) event;
                    QName name = endElement.getName();
                    if ("".equals(name.getNamespaceURI()) && "root".equals(name.getLocalPart())) {
                        return false;
                    }
                }
                return true;
            }
        });
        StringWriter sw = new StringWriter();
        XMLOutputFactory xof = XMLOutputFactory.newInstance();
        XMLEventWriter writer = xof.createXMLEventWriter(sw);
        while (filteredReader.hasNext()) {
            writer.add(filteredReader.nextEvent());
        }
        return sw.toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}