首页 > 技术文章 > 初探XML

E-star 2013-10-29 18:53 原文

百度XML

 

XML:

1:注释,同HTML

2:标记 

3:元素

初探:

首先建立web工程,然后建立xml文件,起名为xmlDemo.xml

<?xml version="1.0" encoding="UTF-8"?>
<xml-body>
	<book>
		<name>Java开发</name>
		<author>刚哥</author>
	</book>
	<book>
		<name>C++开发</name>
		<author>虎哥</author>
	</book>
</xml-body>

  

DOM的简单解析:

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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;

public class ReadXml {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//获得XML文件目录
		File xmlFile = new File("WebRoot/xmlDemo.xml");
		//利用DOM解析我们就需要Document对象, 该对象有DocumentBuilder生成,获得DocumentBuilder
		DocumentBuilder builder = null;
		//DocumentBuilderFactory工厂产生builderFactory
		DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
		try {
			//利用builderFactory产生DocumentBuilder
			builder = builderFactory.newDocumentBuilder();
			try {
				//获得Document
				Document document = builder.parse(xmlFile);
				//利用Document解析XML...
				//解析XML的根元素
				Element root = document.getDocumentElement();
				
				NodeList childNodes = root.getChildNodes();
				for (int i = 0; i < childNodes.getLength(); ++i) {
					Node node = childNodes.item(i);
					if (node.getNodeName().equals("book")) {
						System.out.println("\r\n书籍信息:");
						NodeList nodeDetails = node.getChildNodes();
						for (int j = 0; j < nodeDetails.getLength(); ++j) {
							Node details = nodeDetails.item(j);
							if (details.getNodeName().equals("name")){
								System.out.println("书名:" + details.getTextContent());
							} else if (details.getNodeName().equals("author")) {
								System.out.println("作者:" + details.getTextContent());
							}
						}
					}
				}

			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


注意导包问题..

  

 SAX的简单解析:

import java.io.File;
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.SAXException;
import org.xml.sax.helpers.DefaultHandler;




public class ReadXMLBySAX extends DefaultHandler{
	private String value;
	
	//覆盖characters方法, 该方法用来处理元素中的字符
	public void characters (char ch[], int start, int length)
    throws SAXException
	{
	    value = new String(ch, start, length);
	}
	//覆盖endElement方法,该方法用来处理元素中的字符
	public void endElement (String uri, String localName, String qName) throws SAXException { 
		if (qName.equals("name")) {
			System.out.println("书名" + value);
		} else if (qName.equals("author")) {
			System.out.println("作者" + value);
		}
		System.out.println();
    }
	//覆盖startElement方法,用来处理解析到元素的开始标签
	public void startElement (String uri, String localName,String qName, Attributes attributes) throws SAXException {
		if (qName.equals("book")) {
			System.out.println("书籍信息:");
		}
	}


	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		File xmlFile = new File("WebRoot/xmlDemo.xml");
		SAXParserFactory parserFactory = SAXParserFactory.newInstance();
	    try {
			SAXParser parser = parserFactory.newSAXParser();
			parser.parse(xmlFile, new ReadXMLBySAX());
			
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

  

 

推荐阅读