爱吃咸蛋的超人 2014-03-09 14:21
浏览 797

sax解析xml,Tag的设置

Tag在startElement方法执行后变为book,那在character方法中是如何判断preTag的?

public void startDocument() throws SAXException {

books = new ArrayList();

}

@Override  
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {  
    if("book".equals(qName)){  
        book = new Book();  
        book.setId(Integer.parseInt(attributes.getValue(0)));  
    }  
    preTag = qName;//将正在解析的节点名称赋给preTag  
}  

[size=1em]

@Override

public void characters(char[] ch, int start, int length) throws SAXException {

if(preTag!=null){

String content = new String(ch,start,length);

if("name".equals(preTag)){

book.setName(content);

}else if("price".equals(preTag)){

book.setPrice(Float.parseFloat(content));

}

}

}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 FPGA-SRIO初始化失败
    • ¥15 MapReduce实现倒排索引失败
    • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
    • ¥15 找一位技术过硬的游戏pj程序员
    • ¥15 matlab生成电测深三层曲线模型代码
    • ¥50 随机森林与房贷信用风险模型
    • ¥50 buildozer打包kivy app失败
    • ¥30 在vs2022里运行python代码
    • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
    • ¥15 求解 yolo算法问题