XML, XSL, HTML


The code in this section is that an XSLT transform is used to translate XML input data to HTML output.
Ref: https://docs.oracle.com/javase/tutorial/jaxp/xslt/transformingXML.html
Note - The article1a.xsl, which is from in XSLT examples. The following code is for joy.

 import java.awt.Desktop;
import java.io.*;
import java.net.URISyntaxException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

// For write operation
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;



public class Stylizer {
    // Global value so it can be ref'd by the tree-adapter
    static Document document;
    String root = "ARTICLE";
   
    private Document getDocument() throws ParserConfigurationException{
        Document doc=null;
       
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            doc = docBuilder.newDocument();
            Element rootElement = doc.createElement(root);
            doc.appendChild(rootElement);
            //TITLE
            Element element = doc.createElement("TITLE");
            element.appendChild(doc.createTextNode("A Sample Article"));
            rootElement.appendChild(element);
           
            element = doc.createElement("SECT");
            element.appendChild(doc.createTextNode("The First Major Section"));
            rootElement.appendChild(element);
            Element PARA = doc.createElement("PARA");
            PARA.appendChild(doc.createTextNode("This section will introduce a subsection."));
            element.appendChild(PARA);
            Element element2 = doc.createElement("SECT");
            element2.appendChild(doc.createTextNode("The Subsection Heading"));
            element.appendChild(element2);
            Element PARA2 = doc.createElement("PARA");
            PARA2.appendChild(doc.createTextNode("This is the text of the subsection."));
            element2.appendChild(PARA2);
        return doc;
    }
   
    public static void main(String[] argv) throws URISyntaxException {

        Stylizer stylizer = new Stylizer();
         String argv0 = "src\\com\\xslt\\data\\article1a.xsl";
        String argv1 = "src\\com\\xslt\\data\\article1.xml";

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            File stylesheet = new File(argv0);            File datafile = new File(argv1);
            DocumentBuilder builder = factory.newDocumentBuilder();
            document = builder.parse(datafile);
           
            document = stylizer.getDocument();
            // Use a Transformer for output
            TransformerFactory tFactory = TransformerFactory.newInstance();
            StreamSource stylesource = new StreamSource(stylesheet);
            Transformer transformer = tFactory.newTransformer(stylesource);

            DOMSource source = new DOMSource(document);

            //File
            File f = java.io.File.createTempFile("xml", ".html");
            f.deleteOnExit();
            f.setReadable(true);
            Writer out = new FileWriter(f);
            out.flush();
            System.out.println(":"+f.getAbsoluteFile());
            StreamResult result = new StreamResult(out);            
         
            transformer.transform(source, result);

            Desktop d;
            d = Desktop.getDesktop();
try {
    d.browse(f.toURI());
}catch(Exception e ){
    System.out.println(e.getMessage());
    if(e.getMessage().indexOf("Error message: Unspecified error")!=-1){
       // Rundll32 Shell32.dll,OpenAs_RunDLL file_name.xxx
        Runtime.getRuntime().exec("Rundll32 Shell32.dll,OpenAs_RunDLL"+
                                               " "+ f.toPath().toAbsolutePath().toString());
    }
}
        } catch (TransformerConfigurationException tce) {
            // Error generated by the parser
            System.out.println("\n** Transformer Factory error");
            System.out.println("   " + tce.getMessage());

            // Use the contained exception, if any
            Throwable x = tce;

            if (tce.getException() != null) {
                x = tce.getException();
            }

            x.printStackTrace();
        } catch (TransformerException te) {
            // Error generated by the parser
            System.out.println("\n** Transformation error");
            System.out.println("   " + te.getMessage());

            // Use the contained exception, if any
            Throwable x = te;

            if (te.getException() != null) {
                x = te.getException();
            }

            x.printStackTrace();
        } catch (SAXException sxe) {
            // Error generated by this application
            // (or a parser-initialization error)
            Exception x = sxe;

            if (sxe.getException() != null) {
                x = sxe.getException();
            }

            x.printStackTrace();
        } catch (ParserConfigurationException pce) {
            // Parser with specified options can't be built
            pce.printStackTrace();
        } catch (IOException ioe) {
            // I/O error
            ioe.printStackTrace();
        }
    } // main
}

XML Tools

Altova XMLSpy:http://www.altova.com/
eXcelon Stylus Studio:http://www.stylusstudio.com/
IBM XSLEditor: http://www.alphaworks.ibm.com/tech/xsleditor
XSLFast:http://www.xslfast.com/
Oxygen XML Editor:http://www.oxygenxml.com/
XMLwriter:http://xmlwriter.net/
EditiX:http://free.editix.com/download.html

http://www.topxml.com/
Eclipse plug-in
Orangevolt Eclipse XSLT plug-in http://www.ibm.com/developerworks/opensource/library/os-eclipse-orangevolt/index.html#download

Eclipse Web Tools Platform 3.1 M6
http://www.eclipse.org/webtools/development/news/3.1M6/

评论

此博客中的热门博文

Input in element.eleme.io

Data URI是由RFC 2397 ACE