Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

public class Main {

    public static Document parse(String xml) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            ByteArrayInputStream stream = new ByteArrayInputStream(xml.getBytes("UTF-8"));
            Document document = db.parse(stream);
            return document;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String parse(Document document) {
        try {
            TransformerFactory dbf = TransformerFactory.newInstance();
            Transformer t = dbf.newTransformer();
            t.setOutputProperty("encoding", "utf-8");
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            t.transform(new DOMSource(document), new StreamResult(bos));
            return bos.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}