package com.huawei.t0906_SAX;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SaxParserXml extends DefaultHandler
{
// xml文件的路径
private static final String PATH = "test_book.xml";
private List<Book> books = null;
private Book book;
// 用来记录解析时上一个节点的名称
private String preTag = null;
public static List<Book> getBooks(InputStream is) throws ParserConfigurationException, SAXException, IOException
{
SAXParserFactory sf = SAXParserFactory.newInstance();
SAXParser sp = sf.newSAXParser();
SaxParserXml spx = new SaxParserXml();
sp.parse(is, spx);
return spx.getBooks();
}
private List<Book> getBooks()
{
return books;
}
@Override
public void startDocument() throws SAXException
{
books = new ArrayList<Book>();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException
{
if ("book".equals(qName))
{
book = new Book();
book.setId(attributes.getValue(0));
}
// 将正在解析的节点赋给preTag
preTag = qName;
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException
{
if ("book".equals(qName))
{
books.add(book);
book = null;
}
preTag = null;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException
{
String content = new String(ch, start, length);
if ("name".equals(preTag))
{
book.setName(content);
}
else if ("price".equals(preTag))
{
book.setPrice(Double.valueOf(content));
}
}
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException
{
InputStream is = new SaxParserXml().getClass().getClassLoader().getResourceAsStream(PATH);
List<Book> books = getBooks(is);
for (Book b : books)
{
System.out.println(b);
}
}
}
------------------------DOM----------------------------
package com.huawei.t0906_DOM2;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class DomPaseXml_Element
{
// xml文件路径
private static final String PATH = "test_book.xml";
/**
* dom 解析xml
*
* @param input
* @return
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException [参数说明]
*
* @return List<Book> [返回类型说明]
* @exception throws [违例类型] [违例说明]
* @see [类、类#方法、类#成员]
*/
public static List<Book> getBooks(InputStream input) throws ParserConfigurationException, SAXException, IOException
{
List<Book> books = new ArrayList<Book>();
DocumentBuilderFactory df = DocumentBuilderFactory.newInstance();
DocumentBuilder db = df.newDocumentBuilder();
Document document = db.parse(input);
Element element = document.getDocumentElement();
// 获得book
NodeList bookNodes = element.getElementsByTagName("book");
// book集合的长度
int length1 = bookNodes.getLength();
for (int i = 0; i < length1; i++)
{
Element bookElememt = (Element)bookNodes.item(i);
Book book = new Book();
// 设置图书id
book.setId(bookElememt.getAttribute("id"));
NodeList childNodes = bookElememt.getChildNodes();
// book元素下的长度
int length2 = childNodes.getLength();
for (int j = 0; j < length2; j++)
{
if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
{
if ("name".equals(childNodes.item(j).getNodeName()))
{
book.setName(childNodes.item(j).getFirstChild().getNodeValue());
}
else if ("price".equals(childNodes.item(j).getNodeName()))
{
book.setPrice(Double.valueOf(childNodes.item(j).getFirstChild().getNodeValue()));
}
} // end of - if()
} // end of - for(j)
// 添加图书
books.add(book);
} // end of for(i)
return books;
}
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException
{
// 构建xml文件的数据流
InputStream is = new DomPaseXml_Element().getClass().getClassLoader().getResourceAsStream(PATH);
// 解析xml获得实力数据
List<Book> list = getBooks(is);
for (Book b : list)
{
System.out.println(b.getId() + "-" + b);
}
}
}
分享到:
相关推荐
一个项目同时用dom解析和sax解析xml文件貌似会报错,项目框架建一直是用sax和dom4j解析xml文件的。当我用dom解析xml文件。导入包后就报错识别不了xml文件的编码格式。于是做了一个sax解析xml文件的实例
Sax解析XML文件解
Java SAX解析Xml Demo(详细注释)
Servlet利用SAX解析XML文档,JAX,JSWDP,Servlet利用SAX解析XML文档
Servlet利用SAX解析XML文档(新上传的有源码)Servlet利用SAX解析XML文档(新上传的有源码)Servlet利用SAX解析XML文档(新上传的有源码)Servlet利用SAX解析XML文档(新上传的有源码)Servlet利用SAX解析XML文档(新上传的有...
Sax解析xml文件详细例子,解析复杂节点的xml,把内容显示到view中,例子简单完整,易用
java学习笔记——使用DOM解析XML和使用SAX解析XML
使用SAX方式解析XML SAX 是读取和操作 XML 数据的更快速、更轻量的方 法。SAX 允许您在读取文档时处理它,从而不必等待整个文档被存储之后才采取操作。它不涉及 DOM 所必需的开销和概念跳跃。 SAX API是一个基于事件...
sax解析XML方式的代码及特点,sax解析XML方式的代码及特点,sax解析XML方式的代码及特点,sax解析XML方式的代码及特点
使用sax解析xml的源代码
绝对可用的SAX解析XML文件项目,费了九牛二虎之力,才找到的好资源,特与大家共享出来。直接导入eclipse就可运行,无需任何配置。
JAVA SAX解析XMLJAVA SAX解析XMLJAVA SAX解析XML
SAX解析超大XML文件 示例代码 SAX解析超大XML文件 示例代码 SAX解析超大XML文件 示例代码
sax解析xml本地读取xml,希望对你有用
Android SAX 解析XML SAX 解析XML(通用)
DOM和SAX解析XML文档
NULL 博文链接:https://wang-peng1.iteye.com/blog/1133695
SAX解析XML源码:安卓客户端程序,通过HTTP协议从服务器端获取XML文件,然后解析并输出到控制台
SAX解析xml文件并显示在android虚拟机上实例源码,可以学习一下SAX解析技术
NULL 博文链接:https://mazhihui.iteye.com/blog/1535280