Create an XML file and attach an XSLTag(s): XML XML


import java.io.*;

import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;

public class CreateXML  {

  public static void main (String args[]) {
      new CreateXML().doit();
  }

  public void doit () {
    try{
      Document xmldoc = initXML();
      Element root = xmldoc.getDocumentElement();

      process(xmldoc, root);

      StreamResult out = new StreamResult("howto.xml");
      writeXML(xmldoc, out);
    }
    catch (Exception e) { e.printStackTrace(); }
  }

  public Document initXML() throws ParserConfigurationException{
    // JAXP + DOM
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    DOMImplementation impl = builder.getDOMImplementation();
    return impl.createDocument(null, "HOWTOS", null);
  }

  public void process(Document xmldoc, Element root) {
    Element e0 = xmldoc.createElement("TOPIC");

    Element e1 = xmldoc.createElement("TITLE");
    Node  n1 = xmldoc.createTextNode("Java");
    e1.appendChild(n1);

    Element e2 = xmldoc.createElement("URL");
    Node  n2 = xmldoc.createTextNode
       ("http://www.rgagnon/topics/java-xml.html");
    e2.appendChild(n2);

    e0.appendChild(e1);
    e0.appendChild(e2);
    root.appendChild(e0);
  }

  public void writeXML(Document xmldoc, StreamResult out)
     throws TransformerConfigurationException, TransformerException
  {
    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");
    // we want to pretty format the XML output
    transformer.setOutputProperty
       ("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    //
    transformer.transform(domSource, out);
  }
}
The result is
<?xml version="1.0" encoding="ISO-8859-1"?>
<HOWTOS>
<TOPIC>
<TITLE>Java</TITLE>
<URL>http://www.rgagnon/topics/java-xml.html</URL>
</TOPIC>
</HOWTOS>
And now to attach an XSL
import java.io.*;

import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;

public class CreateXML  {

  public static void main (String args[]) {
      new CreateXML().doit();
  }

  public void doit () {
    try{
      Document xmldoc = initXML();
      Element root = xmldoc.getDocumentElement();

      process(xmldoc, root);

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

      StreamResult out = new StreamResult("howto.xml");
      writeXML(xmldoc, out);
    }
    catch (Exception e) { e.printStackTrace(); }
  }

  public Document initXML() throws ParserConfigurationException{
    // JAXP + DOM
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    DOMImplementation impl = builder.getDOMImplementation();
    return impl.createDocument(null, "HOWTOS", null);
  }

  public void process(Document xmldoc, Element root) {
    Element e0 = xmldoc.createElement("TOPIC");

    Element e1 = xmldoc.createElement("TITLE");
    Node  n1 = xmldoc.createTextNode("Java");
    e1.appendChild(n1);

    Element e2 = xmldoc.createElement("URL");
    Node  n2 = xmldoc.createTextNode
       ("http://www.rgagnon/topics/java-xml.html");
    e2.appendChild(n2);

    e0.appendChild(e1);
    e0.appendChild(e2);
    root.appendChild(e0);
  }

  public void writeXML(Document xmldoc, StreamResult out)
     throws TransformerConfigurationException, TransformerException
  {
    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");
    // we want to pretty format the XML output
    transformer.setOutputProperty
       ("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    //
    transformer.transform(domSource, out);
  }
}
The result is
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="howto.xsl"?>

<HOWTOS>
<TOPIC>
<TITLE>Java</TITLE>
<URL>http://www.rgagnon/topics/java-xml.html</URL>
</TOPIC>
</HOWTOS>

blog comments powered by Disqus