sax解析汉字 java_Java眼中的XML——文件读取

DOM解析

package com.lb;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import org.w3c.dom.NamedNodeMap;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

public class DOMTest {

public static void main(String[] args) throws Exception {

DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();

DocumentBuilder db=dbf.newDocumentBuilder();

Document document=db.parse("src/pac/books.xml");

//获取所有book节点的集合

NodeList bookList=document.getElementsByTagName("book");

/*******获取节点的属性*******/

//遍历book节点

for(int i=0;i

//(不知道节点属性的情况下)

//获取book节点

Node book=bookList.item(i);

//获取book节点的所有属性集合

NamedNodeMap attrs=book.getAttributes();

//遍历book属性

System.out.print("第"+(i+1)+"个节点的属性为: ");

for(int j=0;j

//获取book节点的某一个属性

Node attr=attrs.item(j);

System.out.print(attr.getNodeName()+":"+attr.getNodeValue()+" ");

}

System.out.println();

//(知道节点属性的情况下)

// Element book=(Element) bookList.item(i);

// String idValue=book.getAttribute("id");

// System.out.println("第"+(i+1)+"个节点的id的属性值为:"+idValue);

/*******解析book节点的子节点*******/

//获取book节点

Node books=bookList.item(i);

//获取book节点的子节点集合

NodeList childNodes=books.getChildNodes();

//遍历子节点

System.out.print("第"+(i+1)+"个book节点的内容为:");

for(int k=0;k

Node childNode=childNodes.item(k);

if(childNode.getNodeType()==Node.ELEMENT_NODE){//只取element类型的节点

// System.out.print(childNode.getNodeName()+":"+childNode.getFirstChild().getNodeValue()+" ");//输出book第一个孙节点的Value

System.out.print(childNode.getNodeName()+":"+childNode.getTextContent()+" ");//输出book所有孙节点的Text

}

}

System.out.println();

}

}

}

SAX解析

package com.lb;

import java.util.ArrayList;

import java.util.List;

import org.xml.sax.Attributes;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

public class SAXParserHandler extends DefaultHandler{

String s=null;

Book book=null;

private List bookList=new ArrayList();

public List getBookList() {

return bookList;

}

//每走到一个开始标签都会调用此方法

@Override

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

// TODO Auto-generated method stub

super.startElement(uri, localName, qName, attributes);

//已知了节点属性名

// if(qName.equals("book")){

// System.out.println(attributes.getValue("id"));

// }

//不知节点属性名

if(qName.equals("book")){

book=new Book();

System.out.println("-----------------开始-----------------");

}

if(attributes.getLength()>0){

System.out.print(qName+"节点的属性为:");

for(int i=0;i

System.out.print(attributes.getQName(i)+":"+attributes.getValue(i)+" ");

if(attributes.getQName(i).equals("id")){

book.setId(attributes.getValue(i));//设置id属性值

}

}

System.out.println();

}

if(!qName.equals("book") && !qName.equals("bookstore")){

System.out.print(qName+" ");

}

}

//每走到一个结束标签都会调用此方法

@Override

public void endElement(String uri, String localName, String qName) throws SAXException {

// TODO Auto-generated method stub

super.endElement(uri, localName, qName);

if(qName.equals("book")){

bookList.add(book);

book=null;

System.out.println("----------------结束------------------");

}else if(qName.equals("name")){

book.setName(s);

}else if(qName.equals("year")){

book.setYear(s);

}else if(qName.equals("author")){

book.setAuthor(s);

}else if(qName.equals("language")){

book.setLanguage(s);

}else if(qName.equals("page")){

book.setPage(s);

}

}

@Override

public void startDocument() throws SAXException {

// TODO Auto-generated method stub

super.startDocument();

System.out.println("解析开始");

}

@Override

public void endDocument() throws SAXException {

// TODO Auto-generated method stub

super.endDocument();

System.out.println("解析结束");

}

//获取文本

@Override

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

// TODO Auto-generated method stub

super.characters(ch, start, length);

//获取文字

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

if(!s.trim().equals("")){//.trim()--->去掉字符串边的空格

System.out.println(s);

}

}

}

package com.lb;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

public class SAXTest {

public static void main(String[] args) throws Exception {

SAXParserFactory factory =SAXParserFactory.newInstance();

SAXParser parser=factory.newSAXParser();

SAXParserHandler handler=new SAXParserHandler();

parser.parse("books.xml", handler);

for(int i=0;i

System.out.println(handler.getBookList().get(i));

}

}

}

JDOM解析

package com.lb;

import java.io.FileInputStream;

import java.util.ArrayList;

import java.util.List;

import org.jdom.Attribute;

import org.jdom.Document;

import org.jdom.Element;

import org.jdom.input.SAXBuilder;

public class JDOMTest {

private static List booksList=new ArrayList();

public static void main(String[] args) throws Exception {

SAXBuilder saxBuilder=new SAXBuilder();

// //输出汉字乱码问题:1.修改xml文件的字符集 2.如下方式(转化为指定字符集的文件流)

// Document document=saxBuilder.build(new InputStreamReader(new FileInputStream("src/pac/books.xml"),"utf-8"));

Document document=saxBuilder.build(new FileInputStream("src/pac/books.xml"));

//获取xml文件的根节点(bookstore)

Element element=document.getRootElement();

//获取根节点的所有子节点的List集合

List bookList=element.getChildren();

for(Element book : bookList){

Book bookEntry=new Book();

System.out.println("------开始解析第"+(bookList.indexOf(book)+1)+"本书!------");

/***获取节点属性***/

// // 1.知道属性名的情况

// System.out.println(book.getAttributeValue("id"));

// 2.不知道属性名的时候

//获得book所有属性的集合

List attrList = book.getAttributes();

System.out.print("属性: ");

for (Attribute attr : attrList) {

System.out.print(attr.getName()+":"+attr.getValue()+" ");

if(attr.getName().equals("id")){

bookEntry.setId(attr.getValue());

}

}

System.out.println();

/***获取子节点***/

//获取所有book子节点的集合

List bookChildren = book.getChildren();

System.out.print("节点: ");

for(Element child : bookChildren){

System.out.print(child.getName()+":"+child.getValue()+" ");

if(child.getName().equals("name")){

bookEntry.setName(child.getValue());

}else if(child.getName().equals("year")){

bookEntry.setYear(child.getValue());

}else if(child.getName().equals("author")){

bookEntry.setAuthor(child.getValue());

}else if(child.getName().equals("language")){

bookEntry.setLanguage(child.getValue());

}else if(child.getName().equals("page")){

bookEntry.setPage(child.getValue());

}

}

System.out.println("\n------结束解析第"+(bookList.indexOf(book)+1)+"本书!------");

booksList.add(bookEntry);

bookEntry = null;

}

for(Book bk : booksList){

System.out.println(bk);

}

}

}

----------

DOM4J解析

package com.lb;

import java.io.FileInputStream;

import java.util.ArrayList;

import java.util.List;

import org.jdom.Attribute;

import org.jdom.Document;

import org.jdom.Element;

import org.jdom.input.SAXBuilder;

public class JDOMTest {

private static List booksList=new ArrayList();

public static void main(String[] args) throws Exception {

SAXBuilder saxBuilder=new SAXBuilder();

// //输出汉字乱码问题:1.修改xml文件的字符集 2.如下方式(转化为指定字符集的文件流)

// Document document=saxBuilder.build(new InputStreamReader(new FileInputStream("src/pac/books.xml"),"utf-8"));

Document document=saxBuilder.build(new FileInputStream("src/pac/books.xml"));

//获取xml文件的根节点(bookstore)

Element element=document.getRootElement();

//获取根节点的所有子节点的List集合

List bookList=element.getChildren();

for(Element book : bookList){

Book bookEntry=new Book();

System.out.println("------开始解析第"+(bookList.indexOf(book)+1)+"本书!------");

/***获取节点属性***/

// // 1.知道属性名的情况

// System.out.println(book.getAttributeValue("id"));

// 2.不知道属性名的时候

//获得book所有属性的集合

List attrList = book.getAttributes();

System.out.print("属性: ");

for (Attribute attr : attrList) {

System.out.print(attr.getName()+":"+attr.getValue()+" ");

if(attr.getName().equals("id")){

bookEntry.setId(attr.getValue());

}

}

System.out.println();

/***获取子节点***/

//获取所有book子节点的集合

List bookChildren = book.getChildren();

System.out.print("节点: ");

for(Element child : bookChildren){

System.out.print(child.getName()+":"+child.getValue()+" ");

if(child.getName().equals("name")){

bookEntry.setName(child.getValue());

}else if(child.getName().equals("year")){

bookEntry.setYear(child.getValue());

}else if(child.getName().equals("author")){

bookEntry.setAuthor(child.getValue());

}else if(child.getName().equals("language")){

bookEntry.setLanguage(child.getValue());

}else if(child.getName().equals("page")){

bookEntry.setPage(child.getValue());

}

}

System.out.println("\n------结束解析第"+(bookList.indexOf(book)+1)+"本书!------");

booksList.add(bookEntry);

bookEntry = null;

}

for(Book bk : booksList){

System.out.println(bk);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值