用jdk自带包操作XML
2008-02-23 09:16:28来源:互联网 阅读 ()
写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));
}
}
}
上一篇: Java EE 5规范横空出世
下一篇: 关于>中Tag File的一点补充
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:Java EE 5规范横空出世
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash