XML解析方式
XML总共有4种解析方式
依赖
<module>analysisXml-09</module>
<module>spring-01</module>
</modules>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
1.DOM解析
public static void element(NodeList list) {
for (int i = 0; i < list.getLength(); i++) {
Element element = (Element) list.item(i);
NodeList childNodes = element.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE) {
System.out.print(childNodes.item(j).getNodeName() + ":");
System.out.println(childNodes.item(j).getFirstChild().getNodeValue());
}
}
}
}
public static void node(NodeList list){
for (int i = 0; i <list.getLength() ; i++) {
Node node = list.item(i);
NodeList childNodes = node.getChildNodes();
for (int j = 0; j <childNodes.getLength() ; j++) {
if (childNodes.item(j).getNodeType()==Node.ELEMENT_NODE) {
System.out.print(childNodes.item(j).getNodeName() + ":");
System.out.println(childNodes.item(j).getFirstChild().getNodeValue());
}
}
}
}
public static void main(String[] args) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document d = builder.parse("D:\\code\\Mybatis-Study\\mybatis-09\\src\\main\\resources\\Student.xml");
NodeList sList = d.getElementsByTagName("student");
element(sList);
} catch (Exception e) {
e.printStackTrace();
}
}
2、SAX解析
public class SAXDemoHandel extends DefaultHandler {
@Override
public void startDocument() throws SAXException {
super.startDocument();
System.out.println("sax解析开始");
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
System.out.println("sax解析结束");
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
if (qName.equals("student")){
System.out.println("============开始遍历student=============");
}
else if (!qName.equals("student")&&!qName.equals("class")){
System.out.print("节点名称:"+qName+"----");
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName);
if (qName.equals("student")){
System.out.println(qName+"遍历结束");
System.out.println("============结束遍历student=============");
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException, SAXException {
super.characters(ch, start, length);
String value = new String(ch,start,length).trim();
if (!value.equals("")) {
System.out.println(value);
}
}
public static void main(String[] args) throws Exception {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
SAXDemoHandel handel = new SAXDemoHandel();
saxParser.parse("D:\\code\\Mybatis-Study\\mybatis-09\\src\\main\\resources\\Student.xml",handel);
}
3、DOM4j解析
public static void main(String[] args) throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("D:\\code\\Mybatis-Study\\mybatis-09\\src\\main\\resources\\Student.xml "));
Element rootElement = document.getRootElement();
Iterator iterator = rootElement.elementIterator();
while (iterator.hasNext()) {
Element stu = (Element) iterator.next();
List<Attribute> attributes = stu.attributes();
System.out.println("======获取属性值======");
for (Attribute attribute : attributes) {
System.out.println(attribute.getValue());
}
System.out.println("======遍历子节点======");
Iterator iterator1 = stu.elementIterator();
while (iterator1.hasNext()) {
Element stuChild = (Element) iterator1.next();
System.out.println("节点名:" + stuChild.getName() + "---节点值:" + stuChild.getStringValue());
}
}
}
4、jDOM解析
public static void main(String[] args) throws Exception {
SAXBuilder saxBuilder = new SAXBuilder();
InputStream is = new FileInputStream(new File("D:\\code\\Mybatis-Study\\mybatis-09\\src\\main\\resources\\Student.xml"));
Document document = saxBuilder.build(is);
Element rootElement = document.getRootElement();
List<Element> children = rootElement.getChildren();
for (Element child : children) {
System.out.println("通过rollno获取属性值:"+child.getAttribute("rollno"));
List<Attribute> attributes = child.getAttributes();
for (Attribute attr : attributes) {
System.out.println(attr.getName()+":"+attr.getValue());
}
List<Element> childrenList = child.getChildren();
System.out.println("======获取子节点-start======");
for (Element o : childrenList) {
System.out.println("节点名:"+o.getName()+"---"+"节点值:"+o.getValue());
}
System.out.println("======获取子节点-end======");
}
}