Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4041660
  • 博文数量: 626
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 11080
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-23 13:08
文章分类

全部博文(626)

文章存档

2015年(72)

2014年(48)

2013年(506)

分类: Java

2013-09-17 09:58:24

JDK6笔记(2)----操作XML文件

一、XML文件如下:
文件名为:de_pjxmb.xml


















二、Java源文件如下:
package myfile;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamConstants;

import java.io.FileNotFoundException;
import java.io.FileReader;
public class XmlTest1 {
 public static void main(String[] args) throws XMLStreamException, FileNotFoundException{
  String path="D:/eclipse/Workspace/Test/src/myfile/de_pjxmb.xml";
  FileReader file=new FileReader(path);  
  XMLInputFactory factory=XMLInputFactory.newInstance();
  XMLStreamReader r=factory.createXMLStreamReader(file);
  try{
   int event=r.getEventType();
   while(true){
    switch(event){
    case XMLStreamConstants.START_DOCUMENT: System.out.println("Start Document.");
    break;
    case XMLStreamConstants.START_ELEMENT: System.out.println("Start Element:"+r.getName());
    for(int i=0,n=r.getAttributeCount();i      System.out.println("Attribute: "+r.getAttributeName(i));
    break;
    case XMLStreamConstants.CHARACTERS: 
     if(r.isWhiteSpace()) 
      break;
     System.out.println("Text: "+r.getText());
     break;
    case XMLStreamConstants.END_ELEMENT: System.out.println("End Element:"+r.getName());
    break;
    case XMLStreamConstants.END_DOCUMENT: System.out.println("End Document.");
    break;
    }
    if(!r.hasNext())
     break;
    event=r.next();
   }
  }
  finally{
   r.close();
  }
 } 
}

三、输出结果如下:

Start Document.
Start Element:object
Attribute: title
Attribute: name
Attribute: pname
Attribute: log_table
Attribute: ts_column
Start Element:property
Attribute: title
Attribute: name
Attribute: pname
Attribute: type
End Element:property
Start Element:property
Attribute: title
Attribute: name
Attribute: pname
Attribute: type
End Element:property
Start Element:property
Attribute: title
Attribute: name
Attribute: pname
Attribute: type
End Element:property
Start Element:property
Attribute: title
Attribute: name
Attribute: pname
Attribute: type
Attribute: cc
End Element:property
Start Element:property
Attribute: title
Attribute: name
Attribute: pname
Attribute: type
End Element:property
Start Element:property
Attribute: title
Attribute: name
Attribute: pname
Attribute: type
Attribute: cc
End Element:property
Start Element:property
Attribute: title
Attribute: name
Attribute: pname
Attribute: type
Attribute: cc
End Element:property
Start Element:property
Attribute: title
Attribute: name
Attribute: pname
Attribute: type
Attribute: cc
End Element:property
Start Element:property
Attribute: title
Attribute: name
Attribute: pname
Attribute: type
Attribute: cc
Attribute: cl
End Element:property
Start Element:property
Attribute: title
Attribute: name
Attribute: pname
Attribute: type
End Element:property
Start Element:property
Attribute: title
Attribute: name
Attribute: pname
Attribute: type
End Element:property
Start Element:property
Attribute: title
Attribute: name
Attribute: pname
Attribute: type
End Element:property
Start Element:property
Attribute: title
Attribute: name
Attribute: pname
Attribute: type
Attribute: length
End Element:property
Start Element:property
Attribute: title
Attribute: name
Attribute: pname
Attribute: type
Attribute: length
End Element:property
End Element:object
End Document.

阅读(731) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~