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

简介: JDK6笔记(2)----操作XML文件 一、XML文件如下: 文件名为:de_pjxmb.xml 二、Java源文件如下: package myfile; import javax.

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

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

<?xml version="1.0" encoding="gb2312"?>


二、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.

目录
相关文章
|
6天前
|
XML Java 数据库连接
mybatis中在xml文件中通用查询结果列如何使用
mybatis中在xml文件中通用查询结果列如何使用
8 0
|
8天前
|
XML JavaScript 前端开发
xml文件使用及解析
xml文件使用及解析
|
26天前
|
XML C# 数据格式
使用C#操作XML文件
使用C#操作XML文件
11 0
|
27天前
|
Java
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
9 0
|
29天前
|
Kubernetes Cloud Native Java
Activiti 简介以及最新activiti依赖 pom.xml文件(使用时注意对应版本号)
Activiti 简介以及最新activiti依赖 pom.xml文件(使用时注意对应版本号)
37 1
|
27天前
|
XML Java 数据库连接
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——hibernate的config文件(hibernate.cfg.xml)
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——hibernate的config文件(hibernate.cfg.xml)
10 0
|
2月前
|
XML JavaScript 数据格式
打开 XML 文件报错 There is no Unicode byte order mark
打开 XML 文件报错 There is no Unicode byte order mark
41 0
|
2月前
|
XML 存储 数据格式
什么是 XML 文件的 Schema
什么是 XML 文件的 Schema
25 0
|
22天前
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
14 1
|
26天前
|
XML Java 数据格式
使用java解析XML文件的步骤
使用java解析XML文件的步骤
10 0