Home >Java >javaTutorial >java parsing XML files

java parsing XML files

大家讲道理
大家讲道理Original
2016-11-10 10:31:071546browse

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();                
 DocumentBuilder builder = factory.newDocumentBuilder();      
 Document document = builder.parse(inputStream);      
 Element element = document.getDocumentElement();     
 
 NodeList dataNodes = ((Element)element.getElementsByTagName("Application").item(0)).getChildNodes();
 for(int i = 0; i < dataNodes.getLength(); i++){
     if("Version".equals(dataNodes.item(i).getNodeName())){
         System.err.println("版本信息 : " + dataNodes.item(i).getFirstChild().getNodeValue());
     }else if("ChkUpdate".equals(dataNodes.item(i).getNodeName())){
         System.err.println("版本信息 : " + dataNodes.item(i).getFirstChild().getNodeValue());
     }else if("DownLoad".equals(dataNodes.item(i).getNodeName())){
         System.err.println("版本信息 : " + dataNodes.item(i).getFirstChild().getNodeValue());
     }
 
 }
 
 
 <?xml version="1.0" encoding="UTF-8"?>
 <Applications>
     <Application>
         <Version>1.7</Version>
         <ChkUpdate>baidu.com</ChkUpdate>
         <DownLoad>sohu.com</DownLoad>
     </Application>
 </Applications>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn