XML之JAXP解析

前端之家收集整理的这篇文章主要介绍了XML之JAXP解析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

JAXP:是sun公司开发的一套对xml的解析

它由javax.xml.parsers 、org.w3c.dom 、org.xml.sax包及其子包组成


JAXP有2种解析方式:


一、dom解析:将文档读取进内存,生产Document对象

二、sax解析:从上往下读,读取一行处理一行


dom优点:对文档CRUD方便

缺点:内存消耗大


sax优点:内存消耗小,解析速度快、适合读取文档

缺点:CUD不方便


实例xml文档

<?xml version="1.0" encoding="UTF-8" standalone="no"?><书架>
	<书>
		<书名 id="1234">.Net</书名>
		<作者>wht</作者>
	
	</书>
	<书>
		<书名>javaEE</书名>
		<作者>www</作者>
	</书>
</书架>


dom解析步骤

  1. 创建dom工厂 DocumentBuilderFactory
  2. 得到dom解析器 DocumentBuilder
  3. 读取xml文档,得到 Document
package Jaxp.dom;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Dom {

	//读取文档中的一个指定标签
	@Test
	public void read1() throws Exception{
		//1、创建dom工厂
		DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
		//2、创建解析器
		DocumentBuilder builder=factory.newDocumentBuilder();
		//3、读取xml文件  产生Document对象
		Document document=builder.parse("src/book.xml");
		
		Node node=document.getElementsByTagName("书").item(0);
		
		System.out.println(node.getNodeName());
	}
	
	
	//读取整个文档的标签
	
	@Test
	public void read2() throws Exception{
		//1、创建dom工厂
		DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
		//2、创建解析器
		DocumentBuilder builder=factory.newDocumentBuilder();
		//3、读取xml文件  产生Document对象
		Document document=builder.parse("src/book.xml");
		

		Node root=document.getElementsByTagName("书架").item(0);
		
		list(root);
		
	}


	private void list(Node node) {
		// TODO Auto-generated method stub
		System.out.println(node.getNodeName());
		
		NodeList nodes=node.getChildNodes();
		
		for(int i=0;i<nodes.getLength();i++){
			Node node1=nodes.item(i);
			list(node1);
		}
	}
	
	
	//添加一个标签
	@Test
	public void add() throws Exception{
		//1、创建dom工厂
		DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
		//2、创建解析器
		DocumentBuilder builder=factory.newDocumentBuilder();
		//3、读取xml文件  产生Document对象
		Document document=builder.parse("src/book.xml");
		

		
		Element element=document.createElement("售价");
		element.setTextContent("55元");
		
		Element book=(Element) document.getElementsByTagName("书").item(0);
		book.appendChild(element);
		
		TransformerFactory tFactory=TransformerFactory.newInstance();
		Transformer transformer=tFactory.newTransformer();
		transformer.transform(new DOMSource(document),new StreamResult("src/book.xml"));
	}
	
	//删除一个标签
	
	@Test
	public void delete() throws Exception{
		//1、创建dom工厂
		DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
		//2、创建解析器
		DocumentBuilder builder=factory.newDocumentBuilder();
		//3、读取xml文件  产生Document对象
		Document document=builder.parse("src/book.xml");
		
		
		Element book=(Element) document.getElementsByTagName("书").item(0);
		book.removeChild(document.getElementsByTagName("售价").item(0));
		
		TransformerFactory tFactory=TransformerFactory.newInstance();
		Transformer transformer=tFactory.newTransformer();
		transformer.transform(new DOMSource(document),new StreamResult("src/book.xml"));
	}
	
}


sax解析步骤
  1. 创建sax工厂 SAXParserFactory
  2. 得到sax解析器 SAXParser
  3. 得到读取器 XMLReader
  4. 设置内容处理处理器 reader.setContentHandler()
  5. 读取xml文档
package Jaxp.sax;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

public class Demo {

	
	public static void main(String[] args) throws ParserConfigurationException,SAXException,IOException {
		//1、创建sax工厂
		SAXParserFactory factory=SAXParserFactory.newInstance();
		//2、得到解析器
		SAXParser saxParser=factory.newSAXParser();
		//3、得到读取器
		XMLReader reader=saxParser.getXMLReader();
		//4、设置内容处理器
		reader.setContentHandler(new XMLHandler());
		//5、读取xml文档
		reader.parse("src/book.xml");
		
	}
	
	
}


class XMLHandler implements ContentHandler{

	
	@Override
	public void startElement(String uri,String localName,String qName,Attributes atts) throws SAXException {
		// TODO Auto-generated method stub
		System.out.println("<"+qName+">");
		
		for(int i=0;i<atts.getLength();i++){
			System.out.println(atts.getQName(i));
			System.out.println(atts.getValue(i));
		}
	}

	@Override
	public void endElement(String uri,String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		System.out.println("</"+qName+">");
	}

	@Override
	public void characters(char[] ch,int start,int length)
			throws SAXException {
		// TODO Auto-generated method stub
		System.out.println(new String(ch,start,length));
	}
	
	
	@Override
	public void setDocumentLocator(Locator locator) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void startPrefixMapping(String prefix,String uri)
			throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void endPrefixMapping(String prefix) throws SAXException {
		// TODO Auto-generated method stub
		
	}

	

	@Override
	public void ignorableWhitespace(char[] ch,int length)
			throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void processingInstruction(String target,String data)
			throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void skippedEntity(String name) throws SAXException {
		// TODO Auto-generated method stub
		
	}
	
}

sun公司提供了DefaultHandler

package Jaxp.sax;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

public class Demo2 {

	
	public static void main(String[] args) throws ParserConfigurationException,IOException {
		//1、创建sax工厂
		SAXParserFactory factory=SAXParserFactory.newInstance();
		//2、得到解析器
		SAXParser saxParser=factory.newSAXParser();
		//3、得到读取器
		XMLReader reader=saxParser.getXMLReader();
		//4、设置内容处理器
		reader.setContentHandler(new TagValue());
		//5、读取xml文档
		reader.parse("src/book.xml");
		
	}
	
	
}

//获取第二个  作者的名字
class TagValue extends DefaultHandler{

	private String currentTag;
	private int currentNum;
	private int needTagNum=2;
	
	@Override
	public void startElement(String uri,Attributes attributes) throws SAXException {
		currentTag=qName;
		if(currentTag.equals("作者")){
			currentNum++;
		}
	}
	
	@Override
	public void characters(char[] ch,int length)
			throws SAXException {
		if("作者".equals(currentTag)&¤tNum==needTagNum){
			System.out.println(new String(ch,length));
		}
		
	}

	@Override
	public void endElement(String uri,String qName)
			throws SAXException {
		
		currentTag=null;
	}



	
}

猜你在找的XML相关文章