Thanks.  I got it going.  The main problem was that my parser was not
using a ByteArray input stream.  Secondly, my Xml writer wasn't
generating clean XML.  It work now.  Here's what I got:


        public void writeWordXmlFile() {
                myWords = new ArrayList<Word>();
                myWords.add(new Word("Diversability"));
                myWords.add(new Word("Theate"));

                try {
                        FileOutputStream fos = openFileOutput("mywords.xml",
Context.MODE_PRIVATE);
                        XmlSerializer serializer = Xml.newSerializer();
                        serializer.setOutput(fos, "UTF-8");
                        serializer.startTag("", "Words");

                        for( int i=0; i<myWords.size(); i++) {
                                serializer.startTag("", "WordPair");
                                serializer.startTag("", "word");
                                serializer.text(myWords.get(i).getWord());
                                serializer.endTag("", "word");
                                serializer.startTag("", "count");
                                
serializer.text(Integer.toString(myWords.get(i).getCount()));
                                serializer.endTag("", "count");
                                serializer.endTag("", "WordPair");
                        }
                        serializer.endTag("", "Words");
                        serializer.endDocument();
                        serializer.flush();
                        fos.close();
                        System.out.println("mywords.xml file written.");
                } catch (IOException e) {
                        System.out.println(e);
                }

        }

        public void parseMyWordXmlFile() {
                myWords = new ArrayList<Word>();
                Word wWd;
                String sIn;

                try {
                        System.out.println("Reading mywords.xml");
                        SAXParserFactory factory = 
SAXParserFactory.newInstance();
                        SAXParser saxParser = factory.newSAXParser();
                        DefaultHandler handler = new DefaultHandler() {
                        boolean gotWord = false;
                        boolean gotCount = false;

                        public void startElement(String uri, String localName,
                            String qName, Attributes attributes)
                            throws SAXException {

                                System.out.println("Start Element :" + qName);
                                if (qName.equalsIgnoreCase("word")) {
                                        gotWord = true;
                                }
                                if (qName.equalsIgnoreCase("count")) {
                                        gotCount = true;
                                }
                        }

                        public void endElement(String uri, String localName,
                                        String qName) throws SAXException {
                                System.out.println("End Element :" + qName);
                        }

                        public void characters(char ch[], int start, int length)
                                        throws SAXException {
                                System.out.println(new String(ch, start, 
length));

                                if (gotWord) {
                                        System.out.println("word : " + new 
String(ch, start,
length));
                                        gotWord = false;
                                }

                                if (gotCount) {
                                        System.out.println("Count : "  + new 
String(ch, start,
length));
                                        gotCount = false;
                                }
                        }
                    };

                    FileInputStream is =  openFileInput( "mywords.xml");
                    byte[] byIn = new byte[is.available()];
                    while ( is.read(byIn) != -1 ){
                        System.out.println(new String (byIn));
                    }
                    InputStream inputStream = new ByteArrayInputStream( byIn );
                    Reader reader = new InputStreamReader(inputStream,"UTF-8");
                    InputSource isrc = new InputSource(reader);
                    isrc.setEncoding("UTF-8");
                    saxParser.parse(isrc, handler);
                    is.close();

                } catch (Exception e) {
                        e.printStackTrace();
            }
        }

Chris

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to