3

I want to transform some xml using an xsl-file and output the result somehow (I'm using Android Api Level 8).

My current activity looks like this, but the transformer stays null. LogCat throws an System.err with org.apache.harmony.xml.ExpatParser$ParseException, saying the xml is not well-formed, but I made sure it is.

I found a hint in LogCat that says SystemId Unknown just before the above error message.

What am I doing wrong?

import java.io.OutputStream;

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

import android.app.Activity;
import android.os.Bundle;

public class XsltTester extends Activity {

    private static String TAG = XsltTester.class.getSimpleName();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {

            Source xmlSource = new StreamSource(this.getResources().openRawResource(R.xml.source));
            Source xsltSource = new StreamSource(this.getResources().openRawResource(R.xml.products));

            TransformerFactory transFact = TransformerFactory.newInstance();
            Transformer trans = transFact.newTransformer(xsltSource);
            OutputStream output = new StringOutputStream();
            StreamResult result = new StreamResult(output);
            trans.transform(xmlSource, result);

        } catch (TransformerConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TransformerFactoryConfigurationError e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TransformerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

This is the xml-file to be transformed (source.xml)

<?xml version="1.0"?>
<!-- <?xml-stylesheet href="beatle.xsl" type="text/xsl"?> -->
<person>
 <name>
  <firstname>Paul</firstname>
  <lastname>McCartney</lastname>
 </name>
 <job>Singer</job>
 <gender>Male</gender>
</person>

And this is the corresponding xsl (products.xsl)

<xsl:template match="child::person">
 <html>
  <head>
   <title>
    <xsl:value-of select="descendant::firstname" />
    <xsl:text> </xsl:text>
    <xsl:value-of select="descendant::lastname" />
   </title>
  </head>
  <body>
   <xsl:value-of select="descendant::firstname" />
   <xsl:text> </xsl:text>
   <xsl:value-of select="descendant::lastname" />
  </body>
 </html>
</xsl:template>
</xsl:stylesheet>
1
  • Hi, I'm trying to practice your example for the benefit of my own project but I cannot instantiate or import StringOutputStream. Where is this class from? Have you written it yourself? I'm using API 8 as well. Thank you. Commented Mar 13, 2011 at 17:53

2 Answers 2

0

There really isn't a lot of room for error in such a small piece of code. My guess is that the XML or XSL is to blame after all. There's probably a tiny typo in it. Can you attach the XML/XSLT as well?

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

3 Comments

I added the XML/XSLT. It's just a simple example transformation. I also found a hint in LogCat i also put the question.
The XSL is missing <xsl:stylesheet> at the top.
That's not the mistake, i just forgot to post. Sry :(
0

I just found out what the problem is: I put the XML/XSLT files in res/xml not in res/raw with the result of mangled XML format.

Much ado about nothing :(

Comments

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.