indiWiz.com

Subhash's Tech Log

Archive for the ‘xml’ tag

XSL Transform using Java

without comments

import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;

...

// The XSL source:
StreamSource xsl = new StreamSource(...);

// Create the transformer:
TransformerFactory factory = TransformerFactory.newInstance(); 
Templates template = factory.newTemplates(xsl); // can throw TransformerConfigurationException (parent exception is: TransformerException)
Transformer transformer = template.newTransformer(); // can throw TransformerConfigurationException

// The data XML source
StreamSource data = new StreamSource(...);

// The transformed output:
StreamResult out = new StreamResult(...);

// Transform using the Transformer instance:
transformer.transform(data, out); // can throw TransformerException

Have a look at the StreamSource API and StreamResult API to understand the various ways by which it can be instantiated.

Written by Subhash Chandran

September 14th, 2009 at 4:56 pm

Posted in Java

Tagged with , , , ,

XPath in Java: Code Snippet

without comments

try{
    InputStream is = ...;

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(is);
    is.close();

    XPathFactory xfactory = XPathFactory.newInstance();
    XPath xpath = xfactory.newXPath();
    XPathExpression xpr = xpath.compile("/executors/executor");

    NodeList nodes = (NodeList)xpr.evaluate(doc, XPathConstants.NODESET);
    int size = nodes.getLength();
    for(int i=0; i<size; i++){
        NamedNodeMap nnm = nodes.item(i).getAttributes();
        String strPath = nnm.getNamedItem("path").getNodeValue();
        String strClass = nnm.getNamedItem("class").getNodeValue();
        print("Path / Class: " + strPath + " / " + strClass);
    }
} catch(ParserConfigurationException ex){
    print(ex.getMessage());
} catch(SAXException ex){
    print(ex.getMessage());
} catch(IOException ex){
    print(ex.getMessage());
} catch(XPathExpressionException ex){
    print(ex.getMessage());
}

Written by Subhash Chandran

April 25th, 2009 at 10:11 am

Posted in Java

Tagged with , ,

XOM 1.2.1 Released

without comments

Version 1.2.1 of XOM, the XML parsing Java library written by Elliotte Rusty Harold is released.

Written by Subhash Chandran

March 11th, 2009 at 12:40 pm

Posted in Java,news

Tagged with , ,

Linux command-line XSLT

without comments

To convert an XML based on a XSL, use this command:

$ xsltproc /path/to/xsl.xsl /path/to/xml.xml

The converted document will be written to STDOUT. To write to a particular file, you may use the -o parameter:

$ xsltproc /path/to/xsl.xsl /path/to/xml.xml -o out.html

Check the info/man pages for additional information.

Written by Subhash Chandran

February 2nd, 2009 at 5:28 pm

Posted in Linux

Tagged with , ,