這是讀取XML文件的Java程序,我調試好的。采用的是dom方式讀取XML文件到Vector中。
package src;
import Java.io.*;
import Java.util.Vector;
import Javax.XML.parsers.*;
import org.w3c.dom.*;
public class readXML {
static Document document;
private boolean validating;
public readXML() {
}
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;
readxml my = new readXML();
A = my.toRead("f: omcat5webaPPSmyxmlxmldata9.XML");
for (int i = 0; i < A.size(); i++) {
System.out.println(A.elementAt(i));
}
}
}
這是將XML寫入文件。其中,transformer.setOutputProperty(OutputKeys.ENCODING,"GB2312")關系到編碼問題,非常重要。
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 writeXML {
private Document document;
private String filename;
public writeXML(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{
writexml myxml=new writexml("f: omcat5webaPPSmyxmlxmldata9.XML");
myXML.toWrite("中文題目","中文內容");
myXML.toSave();
System.out.print("Your writing is successful.");
}
catch(ParserConfigurationException exp){
exp.printStackTrace();
System.out.print("Your writing is failed.");
}
}
}