Android: Konwertuj xml za pomocą xslt

Chcę przekształcić jakiś xml za pomocą pliku xsl i jakoś wyprowadzić wynik (używam Android API poziom 8).

Moja obecna aktywność wygląda tak, ale transformator pozostaje zerowy. LogCat rzuca System.err z org.apache.harmony.xml.ExpatParser$ParseException, mówiąc, że xml nie jest dobrze uformowany, ale upewniłem się, że jest.

Znalazłem podpowiedź w LogCat, która mówi SystemId Unknown tuż przed powyższym Komunikatem o błędzie.

Co robię źle?
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();
        }
    }
}

To jest plik XML do przetworzenia (źródło.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>

I to jest odpowiedni xsl (produkty.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>
Author: Aurora, 2010-11-11

2 answers

Naprawdę nie ma zbyt wiele miejsca na błędy w tak małym kawałku kodu. Domyślam się, że winę ponosi XML lub XSL. Prawdopodobnie jest w nim mała literówka. Czy możesz dołączyć również XML / XSLT?

 0
Author: kichik,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-11-11 13:33:49

Właśnie się dowiedziałem w czym problem: pliki XML/XSLT umieszczam w res/xml a nie w Res / raw z wynikiem zniekształconego formatu XML.

Much ado about nothing: (

 0
Author: wonne,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-11-15 11:32:23