用jdk自带包操作XML

写XML:
import org . w3c . dom . * ;
import javax . xml . parsers . * ;
import javax . xml . transform . * ;
import javax . xml . transform . dom . DOMSource ;
import javax . xml . transform . stream . StreamResult ;
import java . io . * ;


public class XmlWrite {
private Document document ;
private String filename;
public XmlWrite( String name ) throws ParserConfigurationException {
filename= name ;
DocumentBuilderFactory factory= DocumentBuilderFactory . newInstance ( ) ;
DocumentBuilder builder= factory. newDocumentBuilder ( ) ;
document = builder. newDocument ( ) ;
}
public void toWrite( String mytitle, String mycontent) {
Element root= document . createElement ( "WorkShop" ) ;
document . appendChild ( root) ;
Element title= document . createElement ( "Title" ) ;
title. appendChild ( document . createTextNode ( mytitle) ) ;
root. appendChild ( title) ;
Element content= document . createElement ( "Content" ) ;
content. appendChild ( document . createTextNode ( mycontent) ) ;
root. appendChild ( content) ;
}
public void toSave( ) {
try {
TransformerFactory tf= TransformerFactory . newInstance ( ) ;
Transformer transformer = tf. newTransformer ( ) ;
DOMSource source = new DOMSource ( document ) ;
transformer . setOutputProperty ( OutputKeys . ENCODING , "GB2312" ) ;
transformer . setOutputProperty ( OutputKeys . INDENT , "yes" ) ;
PrintWriter pw= new PrintWriter ( new FileOutputStream ( filename) ) ;
StreamResult result = new StreamResult ( pw) ;
transformer . transform ( source , result ) ;
}
catch ( TransformerException mye) {
mye. printStackTrace ( ) ;
}
catch ( IOException exp ) {
exp . printStackTrace ( ) ;
}
}
public static void main( String args[ ] ) {
try {
XmlWrite myxml= new XmlWrite( "9.xml" ) ;
myxml. toWrite( "中文题目" , "中文内容" ) ;
myxml. toSave( ) ;
System . out. print ( "Your writing is successful." ) ;
}
catch ( ParserConfigurationException exp ) {
exp . printStackTrace ( ) ;
System . out. print ( "Your writing is failed." ) ;
}
}
}

读XML:
import java . io . * ;
import java . util . Vector ;
import javax . xml . parsers . * ;
import org . w3c . dom . * ;
public class XmlRead {
static Document document ;
private boolean validating;
public XmlRead( ) {
}
public Vector toRead( String filename) {
Vector title= new Vector ( ) ;
Vector content= new Vector ( ) ;
String myStr= new String ( ) ;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory . newInstance ( ) ;
factory. setValidating ( validating) ;
DocumentBuilder builder = factory. newDocumentBuilder ( ) ;
document = builder. parse ( new File ( filename) ) ;
document . getDocumentElement ( ) . normalize ( ) ;
Node node = document . getFirstChild ( ) ;
NodeList list = node . getChildNodes ( ) ;
for ( int i = 0; i < list . getLength ( ) ; i+ + ) {
Node nodeitm = list . item ( i) ;
if ( nodeitm. getNodeName ( ) . equals ( "Title" ) ) {
myStr= nodeitm. getFirstChild ( ) . getNodeValue ( ) ;
title. addElement ( myStr) ; //getFirstChild()

}
if ( nodeitm. getNodeName ( ) . equals ( "Content" ) ) {
myStr= nodeitm. getFirstChild ( ) . getNodeValue ( ) ;
content. addElement ( myStr) ;
}
}
} catch ( Exception exp ) {
exp . printStackTrace ( ) ;
return null ;
}
Vector all= new Vector ( ) ;
all. add ( title) ;
all. add ( content) ;
return all;
}
public static void main( String [ ] args) {
Vector A;
XmlRead my = new XmlRead( ) ;
A = my. toRead( "9.xml" ) ;
for ( int i = 0; i < A. size ( ) ; i+ + ) {
System . out. println ( A. elementAt ( i) ) ;
}
}
}
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值