Example usage for javax.xml.transform OutputKeys INDENT

List of usage examples for javax.xml.transform OutputKeys INDENT

Introduction

In this page you can find the example usage for javax.xml.transform OutputKeys INDENT.

Prototype

String INDENT

To view the source code for javax.xml.transform OutputKeys INDENT.

Click Source Link

Document

indent = "yes" | "no".

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    DOMImplementation impl = builder.getDOMImplementation();
    Document xmldoc = impl.createDocument(null, "TODOs", null);

    Element root = xmldoc.getDocumentElement();
    Element e0 = xmldoc.createElement("TOPIC");
    Element e1 = xmldoc.createElement("TITLE");
    Node n1 = xmldoc.createTextNode("Java");
    e1.appendChild(n1);//  w ww. j  a v  a2s  .  co  m

    Element e2 = xmldoc.createElement("URL");
    Node n2 = xmldoc.createTextNode("http://www.server.com");
    e2.appendChild(n2);
    e0.appendChild(e1);
    e0.appendChild(e2);
    root.appendChild(e0);

    Node pi = xmldoc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"TODOs.xsl\"");
    xmldoc.insertBefore(pi, root);

    StreamResult out = new StreamResult("howto.xml");
    DOMSource domSource = new DOMSource(xmldoc);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(domSource, out);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    // Create a new document
    Document xmlDoc = builder.newDocument();
    // Create root node for the document...
    Element root = xmlDoc.createElement("Players");
    xmlDoc.appendChild(root);/*from   w w  w.  j a v  a2  s . com*/

    // Create a "player" node
    Element player = xmlDoc.createElement("player");
    // Set the players ID attribute
    player.setAttribute("ID", "1");

    // Create currentRank node...
    Element currentRank = xmlDoc.createElement("currentRank");
    currentRank.setTextContent("1");
    player.appendChild(currentRank);

    // Create previousRank node...
    Element previousRank = xmlDoc.createElement("previousRank");
    previousRank.setTextContent("1");
    player.appendChild(previousRank);

    // Create playerName node...
    Element playerName = xmlDoc.createElement("PlayerName");
    playerName.setTextContent("Max");
    player.appendChild(playerName);

    // Create Money node...
    Element money = xmlDoc.createElement("Money");
    money.setTextContent("15");
    player.appendChild(money);

    // Add the player to the root node...
    root.appendChild(player);

    ByteArrayOutputStream baos = null;

    baos = new ByteArrayOutputStream();

    Transformer tf = TransformerFactory.newInstance().newTransformer();
    tf.setOutputProperty(OutputKeys.INDENT, "yes");
    tf.setOutputProperty(OutputKeys.METHOD, "xml");
    tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    DOMSource domSource = new DOMSource(xmlDoc);
    StreamResult sr = new StreamResult(baos);
    tf.transform(domSource, sr);

    baos.flush();
    System.out.println(new String(baos.toByteArray()));
    baos.close();

}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();
    SOAPEnvelope soapEnvelope = soapPart.getEnvelope();

    SOAPHeader soapHeader = soapEnvelope.getHeader();
    SOAPHeaderElement headerElement = soapHeader.addHeaderElement(soapEnvelope.createName("Signature",
            "SOAP-SEC", "http://schemas.xmlsoap.org/soap/security/2000-12"));

    SOAPBody soapBody = soapEnvelope.getBody();
    soapBody.addAttribute(//from  w  w w.j  a  va2  s .c  o  m
            soapEnvelope.createName("id", "SOAP-SEC", "http://schemas.xmlsoap.org/soap/security/2000-12"),
            "Body");
    Name bodyName = soapEnvelope.createName("FooBar", "z", "http://example.com");
    SOAPBodyElement gltp = soapBody.addBodyElement(bodyName);

    Source source = soapPart.getContent();

    Node root = null;
    if (source instanceof DOMSource) {
        root = ((DOMSource) source).getNode();
    } else if (source instanceof SAXSource) {
        InputSource inSource = ((SAXSource) source).getInputSource();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = null;

        db = dbf.newDocumentBuilder();

        Document doc = db.parse(inSource);
        root = (Node) doc.getDocumentElement();
    }

    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(new DOMSource(root), new StreamResult(System.out));
}

From source file:Main.java

private static Transformer getTransformer() throws TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    return transformer;
}

From source file:Main.java

public static String toXMLString(Node node) {
    if (node == null) {
        return "";
    }// w  w  w .  j av  a 2  s  . c  o m
    try {
        Transformer tran = tf.newTransformer();
        tran.setOutputProperty(OutputKeys.INDENT, "yes");
        StringWriter swriter = new StringWriter();
        Source src = new DOMSource(node);
        Result res = new StreamResult(swriter);
        tran.transform(src, res);
        return swriter.getBuffer().toString();
    } catch (Exception e) {
        e.printStackTrace();
        return "";
    }
}

From source file:Main.java

public static byte[] toByteArray(Node doc) {
    try {/*from   w  w  w.ja  va 2 s  .c  om*/
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        StreamResult result = new StreamResult(os);
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);

        return os.toByteArray();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Save a DOM document//from   w  ww .  j a va2 s  .  c  o m
 */
public static void saveDocument(Document document, OutputStream out, boolean indent) throws IOException {
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        if (indent) {
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        }
        transformer.transform(new DOMSource(document), new StreamResult(out));
    } catch (TransformerException e) {
        throw new IOException(e.getMessage());
    }
}

From source file:Main.java

public static void toWriter(Document doc, Writer writer) {
    if (doc == null || writer == null) {
        return;/*from  www.  j a v  a2 s . c  o  m*/
    }
    try {
        Transformer tran = tf.newTransformer();
        tran.setOutputProperty(OutputKeys.INDENT, "yes");
        tran.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        Source src = new DOMSource(doc);
        Result res = new StreamResult(writer);
        tran.transform(src, res);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void serializeDocument(Document doc, OutputStream outputStream) throws Exception {
    TransformerFactory tfactory = TransformerFactory.newInstance();
    Transformer serializer = tfactory.newTransformer();
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    serializer.transform(new DOMSource(doc), new StreamResult(outputStream));
}

From source file:Main.java

public static void encodeAsXml(Node dom, OutputStream os) throws Throwable {
    try {/*from w w  w  .  java2  s  .  c  o  m*/
        DOMSource source = new DOMSource(dom);
        StreamResult result = new StreamResult(os);
        Transformer transformer;
        transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(source, result);
    } catch (TransformerConfigurationException tce) {
        Throwable x = tce;
        if (tce.getException() != null)
            x = tce.getException();
        throw x;
    } catch (TransformerException te) {
        Throwable x = te;
        if (te.getException() != null)
            x = te.getException();
        throw x;
    }
}