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

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

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

<?xml version="1.0" encoding="gb2312"?>
<object title="价格明细表项" name="tbjgmx" pname="TBJGMX" log_table="system.LOG.0002" ts_column="lts">
<property title="ID" name="id" pname="ID" type="int"/>
<property title="时戳" name="lts" pname="LTS" type="int"/>
<property title="工作单ID" name="gzdid" pname="GZDID" type="int"/>
<property title="填报单位" name="tbdw" pname="TBDW" type="int" cc="2"/>
<property title="产品ID" name="cpid" pname="CPID" type="int"/>
<property title="价格类型" name="jglx" pname="JGLX" type="int" cc="3"/>
<property title="结算方式" name="jsfs" pname="JSFS" type="int" cc="17"/>
<property title="包装类型" name="bzlx" pname="BZLX" type="int" cc="6"/>
<property title="市场区域" name="scqy" pname="SCQY" type="int" cc="2" cl="1"/>
<property title="生效标志" name="sxbz" pname="SXBZ" type="int"/>
<property title="生效日期" name="sxrq" pname="SXRQ" type="date"/>
<property title="结束日期" name="jsrq" pname="JSRQ" type="date"/>
<property title="外键" name="fk" pname="FK" type="string" length="40"/>
<property title="外键新" name="fkn" pname="FKN" type="sting" length="40"/>
</object>


二、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<n;++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.
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值