Java與XML(二)用java編寫xml的讀寫程式

來源:互聯網
上載者:User
xml|程式 Java與XML(二)用java編寫xml的讀寫程式

這是讀取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:\\tomcat5\\webapps\\myxml\\xmldata\\9.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:\\tomcat5\\webapps\\myxml\\xmldata\\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.");
}
}
}




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.