Java XAS解析器
无法在下面的代码中找到错误,它正在解析xml,但characters函数没有更新bName和bDescription,它始终显示为零,因此我无法在对象中设置这些值 Java类Java XAS解析器,java,android,sax,saxparser,Java,Android,Sax,Saxparser,无法在下面的代码中找到错误,它正在解析xml,但characters函数没有更新bName和bDescription,它始终显示为零,因此我无法在对象中设置这些值 Java类 /** * */ 包com.tech.reader.utils; 导入java.io.IOException; 导入java.io.InputStream; 导入java.util.ArrayList; 导入javax.xml.parsers.parserConfiguration异常; 导入javax.xml.pars
/**
*
*/
包com.tech.reader.utils;
导入java.io.IOException;
导入java.io.InputStream;
导入java.util.ArrayList;
导入javax.xml.parsers.parserConfiguration异常;
导入javax.xml.parsers.SAXParser;
导入javax.xml.parsers.SAXParserFactory;
导入org.xml.sax.Attributes;
导入org.xml.sax.InputSource;
导入org.xml.sax.SAXException;
导入org.xml.sax.helpers.DefaultHandler;
导入com.tech.reader.R;
导入com.tech.reader.models.Book;
导入com.tech.reader.models.Note;
导入android.content.Context;
/**
*@作者Hafshalrahman
*
*/
公共类NoteParser扩展了DefaultHandler{
ArrayList bookL;
字符串文件名;
注:TMP;
布尔bName=false,bDescription=false;
上下文c;
//SimpleDataFormat sdf=新SimpleDataFormat(“年-月-日”);
公共NoteParser(上下文c,int i){
this.bookXmlFileName=fetchFilename(i);
这个.c=c;
bookL=newarraylist();
解析文档();
}
私有字符串文件名(int i){
字符串s=null;
如果(i==0){
s=“注意”+i+”.xml”;
}
返回s;
}
私有文档(){
//解析
SAXParserFactory=SAXParserFactory.newInstance();
试一试{
SAXParser parser=factory.newSAXParser();
InputStream=null;
//System.out.print(bookXmlFileName);
试一试{
is=c.getResources().openRawResource(R.raw.note1_0);
}捕获(例外e){
System.out.println(e.getMessage()+“无消息”);
}
如果(is!=null)
parser.parse(新的InputSource(is),this);
}捕获(ParserConfiguration异常e){
System.out.println(“ParserConfig错误”);
}捕获(SAXE异常){
System.out.println(“SAXException:xml格式不正确”);
}捕获(IOE异常){
System.out.println(“IO错误”);
}
}
公共ArrayList outputDatas(){
对于(注tmpB:bookL){
System.out.println(tmpB.toString());
}
返回bookL;
}
公共对象[]输出名称(){
ArrayList temp=新的ArrayList();
对于(注tmpB:bookL){
临时添加(tmpB.getName().toString());
}
返回温度toArray();
}
@凌驾
public void startElement(字符串uri、字符串localName、字符串elementName、,
属性)引发SAX异常{
if(elementName.equalsIgnoreCase(“注释”)){
//创建一名新员工并将其放入地图中
字符串id=attributes.getValue(“id”);
//初始化Employee对象并设置id属性
bookTmp=新注释();
//bookTmp.setId(id);
//初始化列表
如果(bookL==null)
bookL=newarraylist();
}else if(elementName.equalsIgnoreCase(“名称”)){
//为字段设置布尔值,将用于设置员工
//变数
bName=true;
}else if(elementName.equalsIgnoreCase(“descr”)){
b说明=true;
}
}
@凌驾
公共void元素(字符串s、字符串s1、字符串元素)
抛出SAX异常{
//如果书尾元素添加到列表中
if(元素等于(“注释”)){
簿记添加(簿记TMP);
//System.out.println(“book added”+bookTmp.getId());
}
}
@凌驾
公共无效字符(char[]ac,int i,int j)引发异常{
System.out.println(“chars=“+新字符串(ac,i,j));
如果(bName){
//年龄元素,设置员工年龄
bookTmp.setName(新字符串(ac,i,j));
bName=false;
}else if(b说明){
bookTmp.setDescription(新字符串(ac,i,j));
b说明=错误;
}
}
}
要读取的XML
注1
看看如何创建应用程序
使用XML。
注2
看看如何创建应用程序
使用XML。
附注3
看看如何创建应用程序
使用XML。
注4
看看如何创建应用程序
使用XML。
附注5
看看如何创建应用程序
使用XML。
附注6
看看如何创建应用程序
使用XML。
在characters函数中,不能保证char[]数组中的数据已准备就绪或已完成。您应该做的是创建一个StringBuilder并将其累积到characters()中。完成后,在endElement()中处理结果
/**
*
*/
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>