2

I am working on a android(2.2) project which needs xsl transformation. The below code works perfectly in a regular non-android java project

public static String transform() throws TransformerException {
    Source xmlInput = new StreamSource(new File("samplexml.xml"));
    Source xslInput = new StreamSource(new File("samplexslt.xslt"));

    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(xslInput); 

    OutputStream baos = new ByteArrayOutputStream();
    Result result = new StreamResult(baos);
    transformer.transform(xmlInput, result);
    return baos.toString();
}

I need similar functionality on android. For this I created 2 files under resources/raw:

  1. samplexml.xml
  2. samplexslt.xslt

(contents of these files come from here.

I tried the below code & it does not work (note the StreamSource constructor arg):

public static String transform() throws TransformerException {
    TransformerFactory factory = TransformerFactory.newInstance();

    Source xmlInput = new StreamSource(this.getResources().openRawResource(R.raw.samplexml));
    Source xslInput = new StreamSource(this.getResources().openRawResource(R.raw.samplexslt));

    Transformer transformer = factory.newTransformer(xslInput);//NullPointerException here
    OutputStream baos = new ByteArrayOutputStream();
    Result result = new StreamResult(baos);
    transformer.transform(xmlInput, result);
}

I saw the spec & believe I need to set a systemId. But I couldn't get the above code to work.

So, in an android project, how to handle xslt transformations? Please provide your thoughts.

2 Answers 2

2

As we know that we Cannot usethisin a static context and you are doing this in your static method transform(). You can do it like this_

public class YourLoadXSLClass extends Activity {
static Resources res;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    res = getResources();

    String strHTML = transform();
    // Other code.....

}

/*
 * Your method that Transform CSLT.
 */
public static String transform() throws TransformerException {
    TransformerFactory factory = TransformerFactory.newInstance();

    // Now your raw files are accessible here.
    Source xmlInput = new StreamSource(
            LoadXSLTinWebview.res.openRawResource(R.raw.samplexml));
    Source xslInput = new StreamSource(
            LoadXSLTinWebview.res.openRawResource(R.raw.samplexslt));

    Transformer transformer = factory.newTransformer(xslInput);

    OutputStream baos = new ByteArrayOutputStream();
    Result result = new StreamResult(baos);
    transformer.transform(xmlInput, result);
    return baos.toString();
 }

}

Here is the complete class code that do the needful. I hope this will help you & all!

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for saving my day ;) I used Strings for the transformer(from another example) which game me errors (no version), but with this answer (including Source) I got the transformationfactory working without any problems!
0

I've never done anything with XSLT but, looking at your code, logically there are only two things that could cause an NPE on that line. The first would be that factory might be null but that doesn't make sense.

That leaves xslInput as being the culprit which suggests openRawResource(R.raw.samplexslt) is failing to return a valid InputStream for the StreamSource constructor to use. Try putting a log statement in such as...

if (xslInput != null {
    Transformer transformer = factory.newTransformer(xslInput);
    ...
}
else
    Log.d("SomeTAG", "xslInput is null!!!");

If it turns out that xslInput is actually null then it suggests openRawResource(...) can't find/process the .xslt file properly. In that case I'd suggest using AssetManagerto open the .xslt file by name...

AssetManager am = this.getAssets();
Source xslInput = new StreamSource(am.open("samplexslt.xslt"));

2 Comments

Thanks for the response. Actually, the "transformer" is coming back as null. Sorry for not being clear on that. Now, I tried the AssetManager approach you suggested, but still get NPE.
OK, that's strange. At the moment I can't think of any reason why that might happen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.