免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1152 | 回复: 0
打印 上一主题 下一主题

用jdk自带包操作XML [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-07-16 09:34 |只看该作者 |倒序浏览

写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));
}
}
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/8780/showart_1083079.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP