第一种解析方法
----------------------------------------------------------------DOM解析方式----------------------------------------------------
public class DOMxml {
List<Tv> list = new ArrayList<Tv>();
public static void main(String[] args) throws Exception {
// 创建一个DocumentBuilderFactory的对象
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// 创建一个DocumentBuilder对象
try {
// 创建一个DocumentBuilder对象
DocumentBuilder db = dbf.newDocumentBuilder();
// 创建DocumentBuilder对象的parse方法加载xml文件
Document document = db.parse("凤凰卫视.xml");
// 获取所有Program节点的集合
NodeList Programlist = document.getElementsByTagName("Program");
// System.out.println(Programlist.getLength());
// 遍历
Tv tv = new Tv();
for (int i = 0; i < Programlist.getLength(); i++) {
Node node = Programlist.item(i);
NodeList nodes = node.getChildNodes();
for (int j = 0; j < nodes.getLength(); j++) {
// 区分textnode以及element类型的node
if (nodes.item(j).getNodeType() == Node.ELEMENT_NODE) {
// Element child=(Element)nodes.item(j);
// System.out.println(child.getTextContent());
// 获取element的节点名
Node node2 = nodes.item(j);
// 获取节点名
String nodeName = node2.getNodeName();
System.out.print(nodeName + ":");
// 获取节点值
// String
// nodeValue=node2.getFirstChild().getNodeValue();
String nodeValue = node2.getTextContent();
// tv.setPlayTime(nodeValue.substring(1));
System.out.println(nodeValue);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
/*DOMxml doMxml=new DOMxml();
doMxml.load();*/
}
/*public void load() throws DocumentException{
Tv tv = new Tv();
InputStream is = this.getClass().getResourceAsStream("/"+"凤凰卫视.xml");
Document doc = new SAXReader().read(is);
Element rootElement = doc.getRootElement();
Element mentElement=(Element)rootElement.elementIterator("ProgramList").next();
for (Iterator<Element> ment=mentElement.elementIterator("Program"); ment.hasNext();) {
Element next = ment.next();
System.out.println(next);
String playtime=next.attributeValue("playTime");
String name=next.attributeValue("name");
String path=next.attributeValue("path");
System.out.println(name);
System.out.println(path);
System.out.println(playtime);
tv.setPlayTime(playtime);
tv.setName(name);
tv.setPath(path);
}*/
}
-------------------------------------------------------第二种SAX解析------------------------------------------------------------------
SAXParserHandler类
public class SAXParserHandler extends DefaultHandler {
int count =0;
String value=null;
Tv tv=null;
private ArrayList<Tv>list=new ArrayList<Tv>();
/**
* 用来解析xml元素
*/
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// 调用DefaultHandler的startElement方法
super.startElement(uri, localName, qName, attributes);
if (qName.equals("Program")) {
// 获取属性值
count++;
System.out.println("-------------开始遍历"+count+"的内容--------------");
int length = attributes.getLength();
// 获取对应节点的属性值
String value = attributes.getValue("");
}else if(!qName.equals("Program")&&!qName.equals("ProgramList")&&!qName.equals("typeB")){
System.out.print("节点名为:"+qName);
}
}
/**
* 用来标识解析开始
*/
@Override
public void startDocument() throws SAXException {
super.startDocument();
System.out.println("--------------SAX解析开始---------------");
}
/**
* 解析结束
*/
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
System.out.println("----------SAX解析结束---------------");
}
/**
* 调用DefaultHandler的endElement方法
*/
@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("Program")) {
//list.add(tv);
System.out.println("-------结束遍历"+count+"的内容---------");
}/*else if (qName.equals("playTime")) {
tv.setPlayTime(value);
}else if (qName.equals("name")) {
tv.setName(value);
}else if (qName.equals("path")) {
tv.setPath(value);
}*/
}
/**
* 获取节点值
*/
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
super.characters(ch, start, length);
value = new String(ch, start, length);
if (!value.trim().equals("")) {
System.out.println("\t节点值为:"+value);
}
}
public ArrayList<Tv> getList() {
return list;
}
}
-------------------main方法--------------------------------
public static void main(String[] args) {
// 获取一个SAXParserFactory实例
SAXParserFactory spf = SAXParserFactory.newInstance();
//通过factory获取SAXParser实例
try {
SAXParser sp=spf.newSAXParser();
//创建SAXParserHandler对象
SAXParserHandler sph=new SAXParserHandler();
sp.parse("凤凰卫视.xml",sph);
//System.out.println("共有"+sph.getList().size());
} catch (Exception e) {
e.printStackTrace();
}
}