• 初探XML


    百度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("
    书籍信息:");
    						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();
    		}
    	}
    
    }
    

      

  • 相关阅读:
    16.10.16学到的JAVA知识
    参数类型转换求和(JAVA)
    大道至简第一篇读后感之愚公移山(伪代码)
    First
    18.10.22 luoguP3374 【模板】树状数组 1
    18.10.16 luoguP3372 线段树模板-区间更新值&求和(POJ3468 A Simple Problem with Integers)
    18.10.16 POJ 2528 Mayor's posters(线段树+离散化)
    18.10.15 POJ 2182 Lost Cows(线段树)
    18.10.10 数算作业-字符串
    18.10.9 不好做的最长上升子序列(nlogn树状数组解LIS)
  • 原文地址:https://www.cnblogs.com/E-star/p/3394948.html
Copyright © 2020-2023  润新知