首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XAS Parser Android

XAS Parser Android
EN

Stack Overflow用户
提问于 2014-06-01 13:51:41
回答 1查看 195关注 0票数 0

在下面的代码中找不到错误,它正在解析xml,但是字符函数没有更新bName和bDescription,它总是显示为零,所以我无法在对象中设置这些值。

Java类

代码语言:javascript
复制
/**
 * 
 */
package com.tech.reader.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.tech.reader.R;
import com.tech.reader.models.Book;
import com.tech.reader.models.Note;

import android.content.Context;

/**
 * @author hafsalrahman
 * 
 */
public class NoteParser extends DefaultHandler {

    ArrayList<Note> bookL;

    String bookXmlFileName;

    Note bookTmp;
    Boolean bName = false, bDescription = false;
    Context c;

    // SimpleDateFormat sdf= new SimpleDateFormat("yy-MM-dd");

    public NoteParser(Context c, int i) {

        this.bookXmlFileName = fetchFilename(i);
        this.c = c;
        bookL = new ArrayList<Note>();

        parseDocument();

    }

    private String fetchFilename(int i) {
        String s = null;
        if (i == 0) {
            s = "note_" + i + ".xml";
        }
        return s;
    }

    private void parseDocument() {

        // parse

        SAXParserFactory factory = SAXParserFactory.newInstance();

        try {

            SAXParser parser = factory.newSAXParser();
            InputStream is = null;
            // System.out.print(bookXmlFileName);
            try {
                is = c.getResources().openRawResource(R.raw.note1_0);
            } catch (Exception e) {
                System.out.println(e.getMessage() + "no message__");
            }

            if (is != null)
                parser.parse(new InputSource(is), this);

        } catch (ParserConfigurationException e) {

            System.out.println("ParserConfig error");

        } catch (SAXException e) {

            System.out.println("SAXException : xml not well formed");

        } catch (IOException e) {

            System.out.println("IO error");

        }

    }

    public ArrayList<Note> outputDatas() {

        for (Note tmpB : bookL) {

            System.out.println(tmpB.toString());

        }
        return bookL;
    }

    public Object[] outputNames() {

        ArrayList<String> temp = new ArrayList<String>();

        for (Note tmpB : bookL) {

            temp.add(tmpB.getName().toString());

        }
        return temp.toArray();
    }

    @Override
    public void startElement(String uri, String localName, String elementName,
            Attributes attributes) throws SAXException {

        if (elementName.equalsIgnoreCase("Note")) {
            // create a new Employee and put it in Map
            String id = attributes.getValue("id");
            // initialize Employee object and set id attribute
            bookTmp = new Note();
            // bookTmp.setId(id);
            // initialize list
            if (bookL == null)
                bookL = new ArrayList<Note>();
        } else if (elementName.equalsIgnoreCase("name")) {
            // set boolean values for fields, will be used in setting Employee
            // variables
            bName = true;
        } else if (elementName.equalsIgnoreCase("descr")) {
            bDescription = true;
        }
    }

    @Override
    public void endElement(String s, String s1, String element)
            throws SAXException {

        // if end of book element add to list

        if (element.equals("note")) {

            bookL.add(bookTmp);
            // System.out.println("book added" + bookTmp.getId());

        }
    }

    @Override
    public void characters(char[] ac, int i, int j) throws SAXException {

        System.out.println("chars=" + new String(ac, i, j));

        if (bName) {
            // age element, set Employee age
            bookTmp.setName(new String(ac, i, j));
            bName = false;
        } else if (bDescription) {
            bookTmp.setDescription(new String(ac, i, j));
            bDescription = false;
        }

    }

}

XML读取

Note1考虑使用XML创建应用程序。Note2考虑使用XML创建应用程序。Note3考虑使用XML创建应用程序。Note4考虑使用XML创建应用程序。Note5考虑使用XML创建应用程序。Note6考虑使用XML创建应用程序。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-01 19:26:30

在您的字符函数中,不能保证char[]数组中的数据已经准备好或完成。您应该做的是创建一个StringBuilder并将其累加到字符()中。完成后,在endElement()中处理结果

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23980569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档