SAX,DOM解析xml文件
import com.testDom.TestDom;
import com.testSax.TestSAX;
public class Main {
public static void main(String[] args) {
String fileName = "student.xml";
System.out.println("SAX解析xml文件");
TestSAX testSAX = new TestSAX();
testSAX.parseXml(fileName);
System.out.println("DOM解析xml文件");
TestDom testDom = new TestDom();
testDom.parseXml(fileName);
}
}
新建xml文件
<sutdents>
<student>
<name>张三</name>
<age>22</age>
<sex>女</sex>
</student>
<student>
<name>李四<span style="display: none; width: 0px; height: 0px;" id="transmark"></span></name>
<age>22</age>
<sex>男</sex>
</student>
</sutdents>
SAX解析方式
package com.testSax;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by yangfan on 2016/3/9
*/
public class TestSAX {
public void parseXml(String fileName) {
try {
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
SAXParser parser = parserFactory.newSAXParser();
InputStream inputStream = new FileInputStream(fileName);
parser.parse(inputStream, new MyHandler());
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
继承父类方法DefaultHandler
package com.testSax;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
/**
* Created by yangfan on 2016/3/9
*/
public class MyHandler extends DefaultHandler {
public MyHandler() {
super();
}
@Override
public void setDocumentLocator(Locator locator) {
super.setDocumentLocator(locator);
}
@Override
public void startDocument() throws SAXException {
System.out.println("文件开始读取");
}
@Override
public void endDocument() throws SAXException {
System.out.println("文件读取结束");
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
System.out.println("start element-----------");
System.out.println(" localName: " + localName);
System.out.println(" qName: " + qName);
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
System.out.println("end element-----------");
System.out.println(" localName: " + localName);
System.out.println(" qName: " + qName);
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
System.out.println(" ch: " + ch);
System.out.println(" start: " + start);
System.out.println(" length: " + length);
}
@Override
public void error(SAXParseException e) throws SAXException {
System.out.println("解析错误");
}
}
DOM解析方式
package com.testDom;
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;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
/**
* Created by yangfan on 2016/3/9
*/
public class TestDom {
public void parseXml(String file) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builderFactory = factory.newDocumentBuilder();
Document document = builderFactory.parse(file);
// 获得文档根元素对对象;
Element root = document.getDocumentElement();
// 获得文档根元素下一级子元素所有元素;
NodeList nodeList = root.getChildNodes();
System.out.println(root.getNodeName());
if (root != null) {
for (int i = 0; i < nodeList.getLength(); i++) {
Node child = nodeList.item(i);
// 输出child的属性;
System.out.println(child);
for (Node node = child.getFirstChild(); node != null; node = node
.getNextSibling()) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
if ("name".equals(node.getNodeName())) {
System.out.println(node.getFirstChild()
.getNodeValue());
}
}
if (node.getNodeType() == Node.ELEMENT_NODE) {
if ("sex".equals(node.getNodeName())) {
System.out.println(node.getFirstChild()
.getNodeValue());
}
}
if (node.getNodeType() == Node.ELEMENT_NODE) {
if ("age".equals(node.getNodeName())) {
System.out.println(node.getFirstChild()
.getNodeValue());
}
}
}
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}