XML Parsing

Hi All,

I am new to XML and XML parsing.

I am having a task of taking a XML file and to generate report in HTML.

What i found out is i can have one XML file and i have to have a XSL

file for it.

Then i have to take XML and XSL as source and generate the HTML result.Can anyone please explain how to proceed with it.

[353 byte] By [java_jamboreea] at [2007-11-26 21:38:04]
# 1
You will want to use XSLT (XSL transformations). There is a set of API classes in java under javax.xml.transform. However, you will need an implementation. Use either Saxon or Xalan.- Saish
Saisha at 2007-7-10 3:20:48 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
thanks Saish for replying.......i understood the XSLT transformation i.e. the XSLT processer will the process the input and give me the output.But please explain me on the implementation part of Xalan and Saxon
java_jamboreea at 2007-7-10 3:20:48 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

Download Xalan from Apache. Then, place xalan.jar (along with xml-apis.jar) on your CLASSPATH and use something like the following:

public final class XslTransformer extends Object {

/////////////////////

// Class Variables //

/////////////////////

private static final Map<String, Templates> STYLESHEET_CACHE;

private static final TransformerFactory FACTORY;

//////////////////

// Constructors //

//////////////////

static {

STYLESHEET_CACHE = new HashMap<String, Templates>();

FACTORY = TransformerFactory.newInstance();

}

private XslTransformer() {

super();

}

////////////////////

// Public Methods //

////////////////////

public static final void transform(final String uriXsl, final InputStream xml, final OutputStream target) {

Templates xslTemplates = getTemplates(uriXsl);

Transformer transformer = null;

try {

transformer = xslTemplates.newTransformer();

}

catch (TransformerConfigurationException fatal) {

throw new ConfigurationError("Unable to create XSL transformer", fatal);

}

try {

transformer.transform(new StreamSource(xml), new StreamResult(target));

}

catch (TransformerException e) {

throw new ParseError("Unable to transform XML using XSL stylesheet", e);

}

}

/////////////////////

// Private Methods //

/////////////////////

private static Templates getTemplates(final String uri) {

Templates templates = STYLESHEET_CACHE.get(uri);

if (templates != null) {

return templates;

}

InputStream in = XslTransformer.class.getResourceAsStream(uri);

if (in == null) {

try {

in = new FileInputStream(uri);

}

catch (FileNotFoundException fatal) {

throw new ConfigurationError("Unable to locate XSL stylesheet at '" + uri + "'", fatal);

}

}

try {

templates = FACTORY.newTemplates(new StreamSource(in));

}

catch (TransformerConfigurationException fatal) {

throw new ConfigurationError("Unable to parse XSL templates from '" + uri + "'", fatal);

}

STYLESHEET_CACHE.put(uri, templates);

return templates;

}

}

- Saish

Saisha at 2007-7-10 3:20:48 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

Actually if you are using Java 1.4 or later, you already have XSLT built in to your Java installation. I believe it is a version of Xalan but it doesn't really matter. The point is that you don't have to download anything and you don't have to mess with your classpath.

But if you are using Java 1.3 then you do need to do the download.

DrClapa at 2007-7-10 3:20:48 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...