欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java 利用dom方式读取、创建xml详解及实例代码

程序员文章站 2024-03-05 10:35:18
java 利用dom方式读取、创建xml详解 1.创建一个接口 xmlinterface.java public interface xmlinterface...

java 利用dom方式读取、创建xml详解

1.创建一个接口

xmlinterface.java

public interface xmlinterface
 {
 
  /** 
  *
 建立xml文档 
  *
 @param filename 文件全路径名称 
  */
  public void createxml(string
 filename); 
  /** 
  *
 解析xml文档 
  *
 @param filename 文件全路径名称 
  */
  public void parserxml(string
 filename); 
}

接口实现

xmlimpl.java

package com.test.xml;
 
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.printwriter;
import javax.xml.parsers.documentbuilder;
import javax.xml.parsers.documentbuilderfactory;
import javax.xml.parsers.parserconfigurationexception;
import javax.xml.transform.outputkeys;
import javax.xml.transform.transformer;
import javax.xml.transform.transformerconfigurationexception;
import javax.xml.transform.transformerexception;
import javax.xml.transform.transformerfactory;
import javax.xml.transform.dom.domsource;
import javax.xml.transform.stream.streamresult;
import org.w3c.dom.document;
import org.w3c.dom.element;
import org.w3c.dom.node;
import org.w3c.dom.nodelist;
import org.xml.sax.saxexception;
 
public class xmlimpl implements xmlinterface{
 private document
 document;
 
 public void init()
 {
  try {
   documentbuilderfactory
 factory = documentbuilderfactory
     .newinstance();
   documentbuilder
 builder = factory.newdocumentbuilder();
   this.document
 = builder.newdocument();
  } catch (parserconfigurationexception
 e) {
   system.out.println(e.getmessage());
  }
 }
 
 public void createxml(string
 filename) {
  element
 root = this.document.createelement("scores"); 
  this.document.appendchild(root); 
  element
 employee = this.document.createelement("employee"); 
  element
 name = this.document.createelement("name"); 
  name.appendchild(this.document.createtextnode("wangchenyang")); 
  employee.appendchild(name); 
  element
 sex = this.document.createelement("sex"); 
  sex.appendchild(this.document.createtextnode("m")); 
  employee.appendchild(sex); 
  element
 age = this.document.createelement("age"); 
  age.appendchild(this.document.createtextnode("26")); 
  employee.appendchild(age); 
  root.appendchild(employee); 
  transformerfactory
 tf = transformerfactory.newinstance();
  try {
   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);
   system.out.println("生成xml文件成功!");
  } catch (transformerconfigurationexception
 e) {
   system.out.println(e.getmessage());
  } catch (illegalargumentexception
 e) {
   system.out.println(e.getmessage());
  } catch (filenotfoundexception
 e) {
   system.out.println(e.getmessage());
  } catch (transformerexception
 e) {
   system.out.println(e.getmessage());
  }
 }
 
 public void parserxml(string
 filename) {
  try {
   documentbuilderfactory
 dbf = documentbuilderfactory.newinstance();
   documentbuilder
 db = dbf.newdocumentbuilder();
   document
 document = db.parse(filename);
    
   nodelist
 employees = document.getchildnodes();
   for (int i
 = 0;
 i < employees.getlength(); i++) {
    node
 employee = employees.item(i);
    nodelist
 employeeinfo = employee.getchildnodes();
    for (int j
 = 0;
 j < employeeinfo.getlength(); j++) {
     node
 node = employeeinfo.item(j);
     nodelist
 employeemeta = node.getchildnodes();
     for (int k
 = 0;
 k < employeemeta.getlength(); k++) {
      system.out.println(employeemeta.item(k).getnodename()
        + ":" +
 employeemeta.item(k).gettextcontent());
     }
    }
   }
   system.out.println("解析完毕");
  } catch (filenotfoundexception
 e) {
   system.out.println(e.getmessage());
  } catch (parserconfigurationexception
 e) {
   system.out.println(e.getmessage());
  } catch (saxexception
 e) {
   system.out.println(e.getmessage());
  } catch (ioexception
 e) {
   system.out.println(e.getmessage());
  }
 }
}

测试

public class main
 {
 
 public static void main(string
 args[]){
  xmlimpl
 dd=new xmlimpl();
  string
 str="d:/grade.xml";
  dd.init();
  dd.createxml(str); //创建xml
  dd.parserxml(str); //读取xml
 }
}

结果

生成xml

<?xml version="1.0" encoding="gb2312"?>
<scores>
<employee>
<name>wangchenyang</name>
<sex>m</sex>
<age>26</age>
</employee>
</scores>

读取xml

生成xml文件成功!
#text:
 
name:wangchenyang
#text:
 
sex:m
#text:
 
age:26
#text:
 
解析完毕

感谢阅读,希望能帮到大家,谢谢大家对本站的支持!

上一篇: java基础之Object类

下一篇: