【问题标题】:SAX pull parser AndroidSAX 拉解析器 Android
【发布时间】:2014-03-29 07:20:44
【问题描述】:

我正在通过 SAX XML 解析器解析一个简单的 XML 文件,并在列表视图中显示结果。这已成功完成。现在我想在 abc 标签像下面的 XML 文件一样关闭时这样做。然后下面的标签项不解析(苹果不添加列表视图)。谁能帮我。提前谢谢

<abc>
   <employee>
    <name>Android</name>
   </employee>
   <employee>
    <name>Nokia</name>
   </employee>
</abc>
   <employee>
    <name>Apple</name>
   </employee>

这是我正在使用的 SAXXMLHandler

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("name")) {
            tempEmp.setName(tempVal);
        }
    }
}

【问题讨论】:

  • 这个xml是你自己创建的吗?
  • 是的,从资产文件夹加载 xml
  • 那不是很好的形式....我正在给你答案的解决方案。

标签: android xml saxparser


【解决方案1】:

维护一个boolean 来控制哪些员工姓名应该添加到列表中,哪些不应该...如下...

public class SAXXMLHandler extends DefaultHandler {

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

    private boolean shouldAdd = false;

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

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


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

        tempVal = "";

        if (qName.equalsIgnoreCase("abc")) {

            shouldAdd = true;

        } else 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("abc") && shouldAdd == true) {

            shouldAdd = false;

        } else if (qName.equalsIgnoreCase("employee") && shouldAdd == true) {
            // add it to the list
            employees.add(tempEmp);
        } else if (qName.equalsIgnoreCase("name")) {
            tempEmp.setName(tempVal);
        }
    }
}

【讨论】:

  • 你不明白我需要什么。我需要添加项目,直到 abc 标签关闭。之后不再在列表视图中添加项目。这可能吗?或者我为此使用其他解析器?
猜你喜欢
  • 2011-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-02
  • 1970-01-01
  • 2012-12-26
  • 2011-12-07
  • 1970-01-01
相关资源
最近更新 更多