• 【自动化__持续集成】___java___XMl___DOM读


    一、代码如下

    package com.wujianbo.five;
    
    import javax.sound.midi.Sequence;
    import javax.xml.bind.Element;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    
    public class Demo08 {
    
    	public static void main(String[] args) throws Exception {
    		// TODO Auto-generated method stub
    		Demo08 demo08= new Demo08();
    		demo08.readXmlByDom();
    
    	}
    	
    	private void readXmlByDom() throws Exception {
    		// TODO Auto-generated method stub
    		DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();
    		DocumentBuilder builder= factory.newDocumentBuilder();
    		
    		String xmlName= System.getProperty("user.dir") + "\data\Students.xml";
    		//System.out.println(xmlName);
    		Document doc= builder.parse(xmlName);
    		doc.normalize();
    		NodeList students= doc.getElementsByTagName("student");
    		//Element s1= (Element)students.item(0);
    		org.w3c.dom.Element s1= (org.w3c.dom.Element)students.item(0);
    		System.out.println(s1.getAttribute("sequence"));
    		
    		Node id1= s1.getElementsByTagName("id").item(0);
    		System.out.println(id1.getFirstChild().getNodeValue());
    		System.out.println("===========================================");
    		for(int i=0;i<students.getLength(); i++) {
    			org.w3c.dom.Element son= (org.w3c.dom.Element)students.item(i);
    			for(Node node= son.getFirstChild(); node!=null; node= node.getNextSibling()) {
    				if(node.getNodeType() == node.ELEMENT_NODE) {
    					String name= node.getNodeName();
    					String value= node.getFirstChild().getNodeValue();
    					System.out.print(name + ": " +value);
    				}
    			}
    			System.out.println();
    		}
    		System.out.println("==========================================");
    	}
    
    }
    
  • 相关阅读:
    android 拖放功能
    android 桌面文件夹ui美化
    instanceof 杂谈
    Android自定义长按事件
    launcher in android
    类似网易163TAB选项卡(标签)代码
    【新手入门教程】简洁纯CSS下拉导航菜单代码
    橘黄色的大气CSS菜单代码
    Flash效果的网站后台左侧Js多级展开菜单代码
    仿FLASH动感十足鼠标滑过放大的菜单代码
  • 原文地址:https://www.cnblogs.com/wujianbo123/p/7492259.html
Copyright © 2020-2023  润新知