XML解析

1、DOM解析XML
<?xml version="1.0" encoding="utf-8"?>
<musices>
	<music>
		<name>Hello</name>
		<size>8622</size>
	</music>
	<music>
		<name>World</name>
		<size>300</size>
	</music>
</musices>	

File f=new File("E:\\music.xml");
	
	DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
	
	DocumentBuilder builder=factory.newDocumentBuilder();
	
	Document doc = builder.parse(f);

	
	NodeList nl = doc.getElementsByTagName("music");//获得名称为music的标签
	for (int i=0;i<nl.getLength();i++){
	System.out.print("歌曲名称" + doc.getElementsByTagName("name").item(i).getFirstChild().getNodeValue());
	System.out.print("歌曲大小:" + doc.getElementsByTagName("size").item(i).getFirstChild().getNodeValue());}

2、JDOM解析XML
<?xml version="1.0" encoding="utf-8"?>
<musices>
	<music>
		<name>Hello</name>
		<size>8622</size>
	</music>
	<music>
		<name>World</name>
		<size>300</size>
	</music>
</musices>	
	SAXBuilder builder = new SAXBuilder();
		Document doc = builder.build(new File("E:\\music.xml"));
		Element foo =  doc.getRootElement();
		List allChildren = foo.getChildren();
		for(int i=0;i<allChildren.size();i++) {
		System.out.print("歌曲名称:" + ((Element)allChildren.get(i)).getChild("name").getText());
		System.out.println("歌曲大小:" + ((Element)allChildren.get(i)).getChild("size").getText());}

3、SAX解析XML
<?xml version="1.0" encoding="utf-8"?>
<musices>
	<music>
		<name>Hello</name>
		<size>8622</size>
	</music>
	<music>
		<name>World</name>
		<size>2000</size>
	</music>
</musices>	
package cn.edu.xml;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MyXmlUtils extends DefaultHandler{
	private boolean isName=false;
	private boolean isSize=false;
	
	private String myname;
	private String mysize;
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		super.characters(ch, start, length);
		if(isName){
			myname=new String(ch,start,length);
		}
		if(isSize)
		{
			mysize=new String(ch,start,length);
		}
		
	}

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

	@Override
	public void endElement(String uri, String localName, String name)
			throws SAXException {
		// TODO Auto-generated method stub
		super.endElement(uri, localName, name);
	if(name.equals("name")){
		System.out.println(myname);
	}else if(name.equals("size")){
		System.out.println(mysize);}
	}

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

	@Override
	public void startElement(String uri, String localName, String name,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		super.startElement(uri, localName, name, attributes);
		if(name.equals("name")){
			isName=true;
			System.out.println("one");
		}
		else if(name.equals("size")){
			isSize=true;
			System.out.println("second");
		}
		
	}

}

4、DOM4J解析XML
<?xml version="1.0" encoding="utf-8"?>
<musices>
	<music>
		<name>Hello</name>
		<size>8622</size>
	</music>
	<music>
		<name>World</name>
		<size>200</size>
	</music>
</musices>	
File f = new File("E:\\music.xml");
		SAXReader reader = new SAXReader();
		Document doc = reader.read(f);
		Element root = doc.getRootElement();
		Element foo;
		for (Iterator i = root.elementIterator("music"); i.hasNext();) {
		foo = (Element) i.next();
		System.out.print("歌曲名称:" + foo.elementText("name"));
		System.out.println(" 歌曲大小:" + foo.elementText("size"));}

你可能感兴趣的:(xml解析)