I'm trying to transform an XML file to Android layout XML through XSLT. So far I got this working, it's returning valid XML. I want this output to be displayed via setContentView (or if this isn't possible, I would like to add Views to a ViewFlipper. So far I haven't been succesfull and I also cannot find any information on this topic.
This is my code:
Source xmlSource = new StreamSource(projectFile); //a XML file on the SD card
Source xsltSource = new StreamSource(getResources().openRawResource(R.raw.session));
TransformerFactory transFact = TransformerFactory.newInstance();
Transformer trans = transFact.newTransformer(xsltSource);
OutputStream output = new StringOutputStream();
StreamResult result = new StreamResult(output);
trans.transform(xmlSource, result);
output.toString() returns the transformed XML in a String format, so far everything works! But 开发者_StackOverflow中文版how can I use this XML in my layout? I've tried the following approaches:
setContentView(output);
and:
projectFlipper = (ViewFlipper)findViewById(R.id.projectFlipper);
projectFlipper.addView(output);
I've tried to cast the variable, but I've been stuck for quite a while now. What am I overlooking? Or is there no possible way to do this?
Thanks in advance!
In fact, view are created via XML files using LayoutInflater. However, as said in the javadoc of this class :
Important : For performance reasons, view inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime.
So, as you see, it's impossible to dynamically use an XML file to create a new view. However, you should be able to translate your "transformation" logic in order to create programmatically your screen.
This is not possible. All the resources are compiled and added to the project apk. What u can do is manually parse the xml and create views on the run.
You can let XSLT build the code for you, so you can dynamic generate the Views
Look for inspiration at:
http://www.javaworld.com/javaworld/jw-02-2002/jw-0201-xslt.html
精彩评论