无法在下面的代码中找到错误,它正在解析xml,但是字符函数未获取bName和bDescription更新,它总是显示为零,所以我无法在我的设置中设置这些值对象
/**
*
*/
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;
}
}
}
<note id="1" > <title> Note1 </title> <descr> look at creating applications with XML. </descr> </note> <note id="2" > <title> Note2 </title> <descr> look at creating applications with XML. </descr> </note> <note id="3" > <title> Note3 </title> <descr> look at creating applications with XML. </descr> </note> <note id="4" > <title> Note4 </title> <descr> look at creating applications with XML. </descr> </note> <note id="5" > <title> Note5 </title> <descr> look at creating applications with XML. </descr> </note> <note id="6" > <title> Note6 </title> <descr> look at creating applications with XML. </descr> </note>
答案 0 :(得分:0)
在字符函数中,无法保证char []数组中的数据已准备就绪或已完成。你应该做的是创建一个StringBuilder并将其累积在characters()中。然后完成后,在endElement()
中处理结果