服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

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.");
   }
   }
  }

扫描关注微信公众号