zoukankan      html  css  js  c++  java
  • xml解析 DOM(JAXP Crimson解析器)

    <?xml version="1.0" encoding="GB2312"?>
    <result>
    <value>
    <no>123</no>
    <addr>beijing</addr>
    </value>
    <value>
    <no>456</no>
    <addr>shanghai</addr>
    </value>
    </result>
    
    package com.xml;
    import java.io.*;  
    import org.w3c.dom.*;  
    import org.xml.sax.SAXException;
    
    import javax.xml.parsers.*;  
    
    public class XMLParseTest 
    {
    	public static void main(String args[])
    	{
    		File xmlFile = new File("c:/test.xml");
    		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    		try {
    			DocumentBuilder builder = factory.newDocumentBuilder();
    			Document doc = builder.parse(xmlFile);
    			NodeList nodeList = doc.getElementsByTagName("value");
    			for(int i=0;i<nodeList.getLength();i++)
    			{
    				System.out.println("车牌号码:"+doc.getElementsByTagName("no").item(i).getFirstChild().getNodeValue());
    				System.out.println("车主地址:"+doc.getElementsByTagName("addr").item(i).getFirstChild().getNodeValue());
    			}
    		} catch (ParserConfigurationException e) {
    			e.printStackTrace();
    		} catch (SAXException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    

  • 相关阅读:
    服务器搭建Git
    BGP协议详解
    以太坊
    燃 * & *
    UML类图解析
    day8.文件操作
    python面试题汇总
    day5.字典
    day5.类型汇总
    day3,4总结程序
  • 原文地址:https://www.cnblogs.com/mengjianzhou/p/5986863.html
Copyright © 2011-2022 走看看