以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 DOM/SAX/XPath 』  (http://bbs.xml.org.cn/list.asp?boardid=11)
----  各位大虾帮忙看看,关于用DOM删除XML中数据  (http://bbs.xml.org.cn/dispbbs.asp?boardid=11&rootid=&id=50127)


--  作者:mianku
--  发布时间:7/17/2007 8:00:00 PM

--  各位大虾帮忙看看,关于用DOM删除XML中数据
package tss;

import javax.xml.parsers.*;
import java.io.*;
import org.xml.sax.*;
import org.w3c.dom.*;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.OutputKeys;


public class test1 {
    public test1() {
    }

    public static void main(String[] args) {
        try {
            File file = new File("student.xml");
            Document dom = DocumentBuilderFactory.newInstance().
                           newDocumentBuilder().parse(file);
            Element el = dom.createElement("student");
            Element els1 = dom.createElement("name");
            Element els2 = dom.createElement("age");

            Text text1 = dom.createTextNode("aaa");
            Text text2 = dom.createTextNode("123");

            els1.appendChild(text1);
            els2.appendChild(text2);

            el.appendChild(els1);
            el.appendChild(els2);
            test1 ts = new test1();
         // ts.inserts(dom, 0, el, "students", file);
         //  ts.updates(dom,"aaaaaaaa","dfsd",file);
         // ts.removes(dom,file,"aaaaaaaa");
            try {
                ts.removes(dom, file, el);
            } catch (FileNotFoundException ex1) {
            } catch (TransformerException ex1) {
            }
        } catch (ParserConfigurationException ex) {
        } catch (IOException ex) {
        } catch (SAXException ex) {
        }
    }

    public void inserts(Document dom, int index, Element el, String push,
                        File file) {
        NodeList ns = dom.getElementsByTagName(push);
        Element n = (Element) ns.item(index);
        n.insertBefore(el, n.getFirstChild());
        try {
            this.toXml(dom, file);
        } catch (TransformerException ex) {
        } catch (FileNotFoundException ex) {
        }
    }

    public void updates(Document dom,String value,String curr,File file) {
      NodeList ns=dom.getElementsByTagName("name");
      Element e;
      for(int i=0;i<ns.getLength();i++){
          e=(Element)ns.item(i);
          if(e.getFirstChild().getNodeValue().equals(curr)){
              e.getFirstChild().setNodeValue(value);
          }
      }
        try {
            this.toXml(dom, file);
        } catch (TransformerException ex) {
        } catch (FileNotFoundException ex) {
        }
        }
        public void removes(Document dom,File file,Element el) throws
            TransformerException, FileNotFoundException {
            el.getFirstChild().removeChild(el);
            this.toXml(dom,file);
        }
//      public void removes(Document dom,File file,String name){
//          NodeList ns=dom.getElementsByTagName("student");
//          Element e;
//          for(int i=0;i<ns.getLength();i++){
//              e=(Element) ns.item(i);
//              if(e.getFirstChild().getNodeValue().equals(name)){
//                  e.getFirstChild().removeChild(e);
//              }
//          }
//        try {
//            this.toXml(dom, file);
//        } catch (TransformerException ex) {
//        } catch (FileNotFoundException ex) {
//        }
//      }

    public void toXml(Document doc, File file) throws FileNotFoundException,
            TransformerException {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        DOMSource dos = new DOMSource(doc);
        StreamResult sr = new StreamResult(new FileOutputStream(file));
        t.setOutputProperty(OutputKeys.ENCODING, "GB2312");
        t.transform(dos, sr);
    }

}

添加和更新都没问题了,就是删除的时候怎么都删除不了,各位大虾帮忙看看
错误出在那里了谢谢了,我目的是删除下面的student元素

<?xml version="1.0" encoding="GB2312"?>
<students>
 <student>
  <name>aaaaaaaa</name>
  <age>23</age>
 </student>
 
 <student>
  <name>aa</name>
  <age>21</age>
 </student>
 <student>
  <name>aaaaaaaa</name>
  <age>23</age>
 </student>
 <student>
  <name>bb</name>
  <age>22</age>
 </student>
 <student>
  <name>aaaaaaaa</name>
  <age>23</age>
 </student>
 
</students>


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
62.500ms