• 【自动化__持续集成】___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("==========================================");
    	}
    
    }
    
  • 相关阅读:
    章节十六、3-TestNG方法和类注解
    章节十六、2-TestNG注解和断言
    章节十六、1-TestNG简介
    章节十五、9-自定义Loggers
    章节十五、8-配置文件File Logging
    章节十五、7- 配置文件-Console Logging
    章节十五、6-log4 2-用默认的配置
    章节十五、5-记录日志---Log4j
    章节十五、4-找到当前页所有连接
    030.[转] sql事务特性
  • 原文地址:https://www.cnblogs.com/wujianbo123/p/7492259.html
Copyright © 2020-2023  润新知