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.
 

posted on 2007-03-14 19:03  YangJin  阅读(128)  评论(0编辑  收藏  举报