全部博文(626)
分类: 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
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.