初探XML
2013-10-29 18:53
239 查看
百度XML
XML:
1:注释,同HTML
2:标记
3:元素
初探:
首先建立web工程,然后建立xml文件,起名为xmlDemo.xml
DOM的简单解析:
SAX的简单解析:
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(); } } }
相关文章推荐
- Spring_Task初探(注解,XML配置)
- 构建安全的Xml Web Service系列之初探使用Soap头
- Swift Protobuf 初探 —— 继 XML 后,JSON 也要被淘汰了吗
- maven pom.xml 配置初探
- Speech SDK 5.1--No.4:用xml描述语法规则初探
- dom4j解析xml、javaweb三层架构初探
- c#基于speech sdk中文限制语音识别XML初探
- 继承ViewGroup研究(2)--在XML中使用ViewGroup初探
- XML解析中的namespace初探
- Swift Protobuf 初探 —— 继 XML 后,JSON 也要被淘汰了吗
- XML初探(二)DTD校验
- AJAX初探,XMLHttpRequest介绍
- XML初探(一)
- 菜鸟初探open xml SDK 2.0
- Tomcat 初探(二) server.xml 配置
- 每天学一点flash (8)as3.0 外部加载xml 初探
- XML在数据库应用中的性能问题初探
- XML解析中的namespace初探
- 构建安全的Xml Web Service系列之初探使用Soap头
- 每天学一点flash (8)as3.0 外部加载xml 初探