java 解析xml文件

dom 解析xml

package com.xmlparse;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class DomXmlParse {

	private String getFile() {
		String path = System.getProperty("user.dir");
		path = path + "\\src\\book.xml";
		return path;
	}

	private void domXmlParses() throws Exception {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();

		Document doc = builder.parse(getFile());

		NodeList list = doc.getElementsByTagName("book");

		//选择性的获取
		for (int i = 0; i < list.getLength(); i++) {

			Node node = list.item(i);

			//获得属性值
			Node attNode = node.getAttributes().getNamedItem("id");
			System.out.println("attribut:"+attNode.getNodeName()+"\t"+attNode.getNodeValue());
		
			//获得节点里面的子节点
			Node n = doc.getElementsByTagName("name").item(i);
			System.out.println("name:"+n.getNodeName()+"\t"+n.getFirstChild().getNodeValue());			
		}		
	}

	public static void main(String[] args) throws Exception {
		new DomXmlParse().domXmlParses();
	}

}




sax 解析xml

package com.xmlparse;

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 SaxXmlParse extends DefaultHandler {

	private String getFile() {
		String path = System.getProperty("user.dir");
		path = path + "\\src\\book.xml";
		return path;
	}

	// 得到元素值

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		String content = new String(ch,start,length);
		
		System.out.println(content);
	}

	// 文档开始

	@Override
	public void startDocument() throws SAXException {
		System.out.println("-------------文档开始-----------------");
	}

	// 元素开始

	@Override
	public void startElement(String uri, String localName, String name,
			Attributes attributes) throws SAXException {
				
		System.out.println("元素属性:"+attributes.getValue("id"));
		System.out.print(name+"\t");
	
	}

	// 文档结束

	@Override
	public void endDocument() throws SAXException {
		System.out.println("---------------文档结束------------");
	}

	// 元素结束

	@Override
	public void endElement(String uri, String localName, String name)
			throws SAXException {		
	}

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

		SAXParserFactory factory = SAXParserFactory.newInstance();
		factory.setValidating(false);
		factory.setNamespaceAware(false);
		
		SaxXmlParse saxparse = new SaxXmlParse();
		
		SAXParser parser = factory.newSAXParser();
		parser.parse(saxparse.getFile(), saxparse);
			
	}

}



xml 文件

<?xml version="1.0" encoding="gb2312"?>
<library>
	<book id="1">
		<name>ejb</name>
		<price>10</price>
		<author>张三</author>
	</book>
	<book id="2">
		<name>jsp开发详解</name>
		<price>78</price>
		<author>小狗</author>
	</book>
	<book id="3">
		<name>struts</name>
		<price>100</price>
		<author>小猪</author>
	</book>
</library>

你可能感兴趣的:(java,jsp,xml,struts,ejb)