SAX解析

Java程序如下:

import org.xml.sax.helpers.DefaultHandler;

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.XMLReader; public class Test7 extends DefaultHandler
{
int indent = 0; public Test7() {
} public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) throws SAXException {
if (qName == "response") {
System.out.println("User: " + atts.getValue("username"));
} else if (qName == "question") {
thisQuestion = atts.getValue("subject");
} thisElement = qName; } public void endElement(String namespaceURI, String localName, String qName)
throws SAXException { thisQuestion = "";
thisElement = "";
}
public void characters(char[] ch, int start, int length)
throws SAXException { if (thisElement == "question") {
printIndent(4);
System.out.print(thisQuestion + ": ");
System.out.println(new String(ch, start, length));
}
} public void startDocument() throws SAXException {
System.out.println("Tallying survey results...");
indent = -4; } public void printIndent(int indentSize) {
for (int s = 0; s < indentSize; s++) {
System.out.print(" ");
}
} String thisQuestion = "";
String thisElement = ""; public static void main (String args[]) { XMLReader xmlReader = null; try { SAXParserFactory spfactory = SAXParserFactory.newInstance(); SAXParser saxParser = spfactory.newSAXParser(); xmlReader = saxParser.getXMLReader();
xmlReader.setContentHandler(new Test7()); InputSource source = new InputSource("surveys.xml");
xmlReader.parse(source); } catch (Exception e) {
System.err.println(e);
System.exit(1);
} }
}

xml文件如下:

<?xml version="1.0"?>
<surveys>
<response username="bob">
<question subject="appearance">A</question>
<question subject="communication">B</question>
<question subject="ship">A</question>
<question subject="inside">D</question>
<question subject="implant">B</question>
</response>
<response username="sue">
<question subject="appearance">C</question>
<question subject="communication">A</question>
<question subject="ship">A</question>
<question subject="inside">D</question>
<question subject="implant">A</question>
</response>
<response username="carol">
<question subject="appearance">A</question>
<question subject="communication">C</question>
<question subject="ship">A</question>
<question subject="inside">D</question>
<question subject="implant">C</question>
</response>
</surveys>

执行效果如下:

Tallying survey results...
User: bob
    appearance: A
    communication: B
    ship: A
    inside: D
    implant: B
User: sue
    appearance: C
    communication: A
    ship: A
    inside: D
    implant: A
User: carol
    appearance: A
    communication: C
    ship: A
    inside: D
    implant: C

sax(用于处理XML事件驱动的推模型)解析例子的更多相关文章

  1. Json/Xml简介和处理模型

    JSON json简介 JSON是一种基于文本的数据交换格式,源自JavaScript,用于Web服务和其他连接的应用程序.以下部分介绍了JSON语法,JSON使用概述以及生成和解析JSON的最常用方 ...

  2. XML概念定义以及如何定义xml文件编写约束条件java解析xml DTD XML Schema JAXP java xml解析 dom4j 解析 xpath dom sax

    本文主要涉及:xml概念描述,xml的约束文件,dtd,xsd文件的定义使用,如何在xml中引用xsd文件,如何使用java解析xml,解析xml方式dom sax,dom4j解析xml文件 XML来 ...

  3. Paip.Php Java 异步编程。推模型与拉模型。响应式(Reactive)”编程FutureData总结... 1

    Paip.Php  Java 异步编程.推模型与拉模型.响应式(Reactive)"编程FutureData总结... 1.1.1       异步调用的实现以及角色(:调用者 提货单) F ...

  4. XML的概述,.Dom4解析和SAX解析

    1.什么是XML XML 指可扩展标记语言(EXtensible Markup Language) html(hyper text markup langauge) XML 是一种标记语言,很类似 H ...

  5. XML的DOM、SAX、DEMO4J及DEMO4J整合Path的代码例子

    1.DMO解析 package cn.itcast.xml.dom; import java.io.File; import javax.xml.parsers.DocumentBuilder; im ...

  6. 在Flex中用于处理XML对象的E4X 方法

    ECMAScript for XML 规范定义了一组用于使用 XML 数据的类和功能.这些类和功能统称为 E4X.ActionScript 3.0 包含以下 E4X 类:XML.XMLList.QNa ...

  7. XML文件的创建和解析笔记

    解析XML的四种方法 XML现在已经成为一种通用的数据交换格式,它的平台无关性,语言无关性,系统无关性,给数据集成与交互带来了极大的方便.对于XML本身的语法知识与技术细节,需要阅读相关的技术文献,这 ...

  8. Comet:基于 HTTP 长连接的“服务器推”技术解析

    原文链接:http://www.cnblogs.com/deepleo/p/Comet.html 一.背景介绍 传统web请求,是显式的向服务器发送http Request,拿到Response后显示 ...

  9. Caffe学习笔记(一):Caffe架构及其模型解析

    Caffe学习笔记(一):Caffe架构及其模型解析 写在前面:关于caffe平台如何快速搭建以及如何在caffe上进行训练与预测,请参见前面的文章<caffe平台快速搭建:caffe+wind ...

随机推荐

  1. GDI+ 两个汇总 : 为什么CImage类别是根据GDI+的?

    在很多资料上都说CImage类是基于GDI+的,可是为什么是基于GDI+的呢? 由于使用这个类时,并没有增加#include <gdiplus.h> .也没有在程序開始和结束时分别写GDI ...

  2. java IdentityHashMap 与HashMap

    这两个map的主要区别在于,比较key值什么时候: IdentityHashMap我觉得当k1 == k2 时刻key值一样的 HaspMap觉得k1 == null ? k2 == null:k1. ...

  3. HDU2149-Public Sale

    Public Sale Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  4. [译]MVC应用程序生命周期

    原文:MVC Application Lifecycle 来一探究竟在MVC应用程序中参与请求处理的各个不同组件. 目录: 序言 背景 UrlRoutingModule RouteHandler Mv ...

  5. ASP.NET MVC4实现URL伪静态

    1.在Web.config添加节点配置: <system.webServer> <modules runAllManagedModulesForAllRequests="t ...

  6. 它的斗争“和loser对话”短篇故事

    今天,一个朋友发来的图片故事,尽管听说过,但见一.仍感慨颇多. 有时总是说easy,其实做起来的另一个故事. 想实现梦想,看来还是要脚踏实地,一步一步.不断努力,不断前行啊! 版权声明:本文博客原创文 ...

  7. 【iOS开展-50】使用它来创建一个新的类的实现代码包,因此,不自觉地练习简单MVC实验,附带动画

    接下来说说代码封装最后一个个案. 最后一种情况看:[iOS开展-48]九宫格案例:自己主动布局.字典转模型运用.id和instancetype差别.xib反复视图运用及与nib关系 (1)代码封装的原 ...

  8. git merge简介(转)

    git merge的基本用法为把一个分支或或某个commit的修改合并现在的分支上.我们可以运行git merge -h和git merge --help查看其命令,后者会直接转到一个网页(git的帮 ...

  9. requirejs实现模块化

    使用requirejs实现模块化编程 > 序言 - -# 公司大了,业务多了,前端代码量也逐渐增大,我们渐渐的依赖js实现的交互越来越多,长期以来会导致我们的代码维护越来越困难,所以依赖的插件也 ...

  10. Matlab与.NET基于类型安全的接口混合编程入门

    原文:[原创]Matlab与.NET基于类型安全的接口混合编程入门 如果这些文章对你有用,有帮助,期待更多开源组件介绍,请不要吝啬手中的鼠标. [原创分享]Matlab.NET混编调用Figure窗体 ...