Xml处理【Java版】

一、读取Xml文档

       xml文档内容:

 

       代码实现:

1 try{
2 File myFile=new File("E:\\SourceCode\\JavaWorkspace\\Xml\\XmlFiles\\Candidate.xml");
3 DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
4 DocumentBuilder builder=factory.newDocumentBuilder();
5 Document doc = builder.parse(myFile);
6 NodeList myInformationNodeList = doc.getElementsByTagName("information");
7 for (int i=0;i<myInformationNodeList.getLength();i++){
8 System.out.println("姓名:" + doc.getElementsByTagName("name").item(i).getFirstChild().getNodeValue());
9 System.out.println("开发语言:" + doc.getElementsByTagName("language").item(i).getFirstChild().getNodeValue());
10 }
11 }catch(Exception e){
12 e.printStackTrace();
13 }

        输出结果:

      

posted @ 2011-06-03 00:23  edisonfeng  阅读(250)  评论(0编辑  收藏  举报