Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
Benjen
Posts: 33
Registered: ‎05-12-2010
My Device: none
Accepted Solution

XSL file to format XML Document

Hey programers,

 

This thread makes reference to this one (http://supportforums.blackberry.com/t5/Java-Development/Reading-XML-document/m-p/512637) which was solved yesterday.

 

A little background : i have successfully retrieved XML code from a server as a response to a post message, I was able to put it inside a document using InputStream and was able to read the text content of that XML code on the Blackberry screen.

 

Now I would like to use a .xsl file to format this XML document and then show it on the screen as HTML. Is this possible? How would I go at doing that?

 

I have found this piece of code on the internet (related to java, not blackberry specific):

 

public class CreationHTML{
    public static void createHTML(String xml, String xsl, String html) throws Exception{
        // Creation of the DOM source
        DocumentBuilderFactory fabriqueD = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = fabriqueD.newDocumentBuilder();
        File fileXml = new File(xml);
        Document document = builder.parse(fileXml);
        Source source = new DOMSource(document);

        // Creation of the output file
        File fileHtml = new File(html);
        Result resultat = new StreamResult(fileHtml);

        // Configuration of the transformer
        TransformerFactory fabriqueT = TransformerFactory.newInstance();
        StreamSource stylesource = new StreamSource(xsl);
        Transformer transformer = fabriqueT.newTransformer(stylesource);
        transformer.setOutputProperty(OutputKeys.METHOD, "html");
        
        // Transformation
        transformer.transform(source, resultat);
    }
    public static void main(String[] args){
        try{
            createHTML("Annuaire.xml", "Annuaire.xsl", "Annuaire.html");
        }catch(Exception e){e.printStackTrace();}
    }
}

But I actually have a .xsl file, not a String, do you think I could adapt it to suit my need?

 

Thanks a lot for any help!

Please use plain text.
Developer
simon_hain
Posts: 16,189
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: XSL file to format XML Document

if you implement TransformerFactory on the blackberry: sure, no problem.

Otherwise: Rather not.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Contributor
Benjen
Posts: 33
Registered: ‎05-12-2010
My Device: none

Re: XSL file to format XML Document

Thanks for your answer Simon. :smileyhappy:

 

How should I do then if I choose not to use the TransformerFactory, is there any way to use .xsl files with the Blackberry?

 

Regarding SAX (discussed in the mentioned thread in the first post), what do SAX do exactly, is it similar to .xsl formating?

 

Sorry for all those dumb question but I really don't know much about all this...

 

Anyway, thanks again, we're getting closer!

Please use plain text.
Developer
simon_hain
Posts: 16,189
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: XSL file to format XML Document

no, saxparser is just a different xml parser impl (pull parser).

 

you would have to write the whole html generation yourself i guess - would be a lot easier to do this server-side.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Contributor
Benjen
Posts: 33
Registered: ‎05-12-2010
My Device: none

Re: XSL file to format XML Document

Yes I guess you are right, thanks again for your precious advices!

Please use plain text.