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 (Transfor开发者_JS百科merException 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
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.
精彩评论