Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android SAX解析器新手,所以需要这个基本的_Android_Sax_Saxparser - Fatal编程技术网

Android SAX解析器新手,所以需要这个基本的

Android SAX解析器新手,所以需要这个基本的,android,sax,saxparser,Android,Sax,Saxparser,如果我们在SAX解析器中执行此操作: public class SAXXMLHandler extends DefaultHandler { private List<Employee> employees; private String tempVal; private Employee tempEmp; public SAXXMLHandler() { employees = new ArrayList<Employee&

如果我们在SAX解析器中执行此操作:

public class SAXXMLHandler extends DefaultHandler {

    private List<Employee> employees;
    private String tempVal;
    private Employee tempEmp;

    public SAXXMLHandler() {
        employees = new ArrayList<Employee>();
    }

    public List<Employee> getEmployees() {
        return employees;
    }

    // Event Handlers
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        // reset
        tempVal = "";
        if (qName.equalsIgnoreCase("employee")) {
            // create a new instance of employee
            tempEmp = new Employee();
        }
    }

    public void characters(char[] ch, int start, int length)
            throws SAXException {
        tempVal = new String(ch, start, length);
    }

    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        if (qName.equalsIgnoreCase("employee")) {
            // add it to the list
            employees.add(tempEmp);
        } else if (qName.equalsIgnoreCase("id")) {
            tempEmp.setId(Integer.parseInt(tempVal));
        } else if (qName.equalsIgnoreCase("name")) {
            tempEmp.setName(tempVal);
        } else if (qName.equalsIgnoreCase("department")) {
            tempEmp.setDepartment(tempVal);
        } else if (qName.equalsIgnoreCase("type")) {
            tempEmp.setType(tempVal);
        } else if (qName.equalsIgnoreCase("email")) {
            tempEmp.setEmail(tempVal);
        }
    }
}
公共类SAXXMLHandler扩展了DefaultHandler{
私人名单雇员;
私有字符串tempVal;
私人雇员临时管理计划;
公共SAXXMLHandler(){
employees=newarraylist();
}
公开名单{
返回员工;
}
//事件处理程序
public void startElement(字符串uri、字符串localName、字符串qName、,
属性)引发SAX异常{
//重置
tempVal=“”;
if(qName.equalsIgnoreCase(“员工”)){
//创建employee的新实例
tempEmp=新员工();
}
}
公共无效字符(字符[]ch,整数开始,整数长度)
抛出SAX异常{
tempVal=新字符串(ch、start、length);
}
公共void endElement(字符串uri、字符串localName、字符串qName)
抛出SAX异常{
if(qName.equalsIgnoreCase(“员工”)){
//将其添加到列表中
添加(tempEmp);
}else if(qName.equalsIgnoreCase(“id”)){
tempEmp.setId(Integer.parseInt(tempVal));
}else if(qName.equalsIgnoreCase(“名称”)){
tempEmp.setName(tempVal);
}else if(qName.equalsIgnoreCase(“部门”)){
临时行政部门(临时行政部门);
}else if(qName.equalsIgnoreCase(“类型”)){
tempEmp.setType(tempVal);
}else if(qName.equalsIgnoreCase(“电子邮件”)){
tempEmp.setEmail(tempVal);
}
}
}
为此:

<employee>
        <id>2163</id>
        <name>Kumar</name>
        <department>Development</department>
        <type>Permanent</type>
        <email>kumar@tot.com</email>
</employee>
<MyResource>
<Item>First</Item>
<Item>Second</Item>
</MyResource>

2163
古玛
发展
永久的
kumar@tot.com
我们将在SAX解析器中为此做什么:

<employee>
        <id>2163</id>
        <name>Kumar</name>
        <department>Development</department>
        <type>Permanent</type>
        <email>kumar@tot.com</email>
</employee>
<MyResource>
<Item>First</Item>
<Item>Second</Item>
</MyResource>

弗斯特
第二
我是SAX解析器的新手。
以前我对同一个XML有问题。

没有为解析这个简单的XML而构建的解析器。

要解析名为
MyResource
的条目,它包含
项的多个条目
,可以执行以下操作:

首先,在startDocument方法中初始化变量,以允许重用处理程序:

private List<MyResource> resources;
private MyResource currentResource;
private StringBuilder stringBuilder;

public void startDocument() throws SAXException {
    map = null;
    employees = new ArrayList<Employee>();
    stringBuilder = new StringBuilder();
}
需要使用
characters
方法读取每个标记的内容。 使用StringBuilder读取字符。SAX可以为每个标记多次调用characters方法:

public void characters(char[] ch, int start, int length) throws SAXException {
    stringBuilder.append(ch, start, length);
}
endElement
中,每次命名关闭的标记
Item
并创建MyResource时(即,您在某处有MyResource的实例),都要创建一个新的

当closed标记为
MyResource
时,将其添加到结果列表中并清除临时变量

public void endElement(String uri, String localName, String qName) throws SAXException {
    if("MyResource".equals(qName)) {
        resources.add(currentResource);
        currentResource = null;

    } else if(currentResource != null && "Item".equals(qName)) {
        currentResource.addItem(new Item(stringBuilder.toString()));
    }
}
我假设您在
MyResource
中有
项目的
列表

不要忘记在解析后添加一个方法来检索资源:

List<MyResources> getResources() {
    return resources;
}
List getResources(){
归还资源;
}

您能为我的XML提供代码吗?因为它只包含
标记,没有id、名称等标记。例如,
标记名重复出现,这与我提到的员工示例中的情况不同…我对SAX完全陌生。。。