0

I was working on xslt on android. The users.xml file:

<?xml version="1.0" encoding="utf-8"?>
<users>
  <user>
    <fname>somename</fname>
    <hobbies>
      <hobby>Movie</hobby>
      <hobby>Trekking</hobby>
    </hobbies>
  </user>
</users>

The users.xsl file

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <html>
            <body>
                <xsl:for-each select="users/user">
                    <h2>
                        <xsl:value-of select="fname" />
                    </h2>
                    <h3>Hobbies :</h3>
                    <xsl:for-each select="hobbies/hobby">
                        <xsl:value-of select="." />
                        <xsl:if test="position() != last()">
                            <xsl:text> , </xsl:text>
                        </xsl:if>
                    </xsl:for-each>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

android layout userview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <WebView
        android:id="@+id/userwebview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

Android activity

public class UserDisplayActivity extends Activity {
    WebView userView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.userwebview);
        userView = (WebView) findViewById(R.id.userwebview);
        loadTransformedHtml();
    }
    private void loadTransformedHtml() {
        try {
            String htmlTransformed=UserXmlTransform.getTransformedHtml();
            userView.loadData(htmlTransformed, "text/html", "utf-8");
        } catch (TransformerException e) {
            e.printStackTrace();
        }
    }
 }

And UserXmlTransform class

public class UserXmlTransform {
    static final String sdPath=Environment.getExternalStorageDirectory().getAbsolutePath();
    static final File xmlFileF = new File(sdPath+"/users.xml");
    static final File xsltFileF = new File(sdPath+"/users.xsl");
    public static String getTransformedHtml() throws TransformerException {
        Source srcXml = new StreamSource(xmlFileF);
        Source srcXsl = new StreamSource(xsltFileF);
        StringWriter writer = new StringWriter();
        Result result = new StreamResult(writer);
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer(srcXsl);
        transformer.transform(srcXml, result);
        return writer.toString();
    }
}

The transformation is successfully run with UserXmlTransform.java code while testing as java project with exactly same xml and xsl file.

In android app, the files are at appropriate location. But while running the NullPointer Exception is thrown at line

transformer.transform(srcXml, result); 

of UserXmlTransform.java. why this transformer object became null in android.

I could not figure out what is problem. Please Help me. [Added] I am using SDK 2.2

gtiwari333
  • 24,554
  • 15
  • 75
  • 102
  • 1
    Your transformer is null. I would suggest running this in the debugger and comparing the behavior on the emulator with that of the native version. – Sky Kelsey Sep 20 '11 at 04:44
  • i used scanner to print the content of xml and xsl files. everything is ok. But this line : Transformer transformer = tFactory.newTransformer(srcXsl); returns null for transformer object. – gtiwari333 Sep 20 '11 at 05:38
  • 1
    Maybe this will help? Perhaps the Transformer class is missing fromy your Android SDK? http://stackoverflow.com/questions/5533280/creating-an-xml-document-in-android – Sky Kelsey Sep 20 '11 at 06:29
  • 1
    I think the error is due to the order of attributes 'version' and 'xmlns:xsl'. I change the order of these attributes and problem got solved. – gtiwari333 Sep 20 '11 at 06:57

1 Answers1

1

I did the following change in users.xsl :

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

To

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

And the problem was solved.

But still don't know why this caused the error.

gtiwari333
  • 24,554
  • 15
  • 75
  • 102