熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

對XML文檔進行添、刪、改、查的程序

2013-11-13 09:52:56  來源: .NET編程 
    下面寫了個對XML文檔進行添查的程序
    先是xml文檔代碼如下:

     <?xml version= encoding=utf?><studentInfo>
    <student>
    <name id=>aa</name>
    <age></age>
    <sex>男</sex>
    <score></score>
    </student>
    <student>
    <name id=>bb</name>
    <age></age>
    <sex>女</sex>
    <score></score>
    </student>
    <student>
    <name id=>cc</name>
    <age></age>
    <sex>女</sex>
    <score></score>
    </student>
    </studentInfo>

    然後就是java應用程序注解已說明首先要在外面下載個包xercesjar(當然也可用Eclipse中自帶的只是代碼稍稍不同)代碼如下:

 package comDome;
    import javaioBufferedReader;
    import javaioFileReader;
    import javaxxmltransformTransformer;
    import javaxxmltransformTransformerFactory;
    import javaxxmltransformdomDOMSource;
    import javaxxmltransformstreamStreamResult;
    import orgapachexercesparsersDOMParser;
    import orgwcdomAttr;
    import orgwcdomDocument;
    import orgwcdomElement;
    import orgwcdomNamedNodeMap;
    import orgwcdomNode;
    import orgwcdomNodeList;
    import orgwcdomText;
    import orgxmlsaxInputSource;
    public class DomeTest {
    /*
    * 此方法為得到Document對象實例
    */
    public static Document getInstance(String xmlPath) {
    Document doc = null;
    try {
      BufferedReader bf = new BufferedReader(new FileReader(xmlPath));
      DOMParser ps = new DOMParser();// xml解析器
      psparse(new InputSource(bf));// 解析xml
      doc = psgetDocument();// 獲得Document對象
    } catch (Exception e) {
      eprintStackTrace();
    }
    return doc;
    }
    /*

        * 此方法用來打印元素等

     */
    public static void printAll(Document doc) {
    NodeList nl = docgetElementsByTagName(*);
    Node n;
    for (int i = ; i < nlgetLength(); i++) {
      n = em(i);
      Systemoutprintln(ngetNodeName() + );
    }
    }
    /*

    * 此方法用來打印屬性

 */
    public static void printAttr(Document doc) {
    NodeList nl = docgetElementsByTagName(*);
    Element e;
    Attr att;// 屬性對象
    NamedNodeMap nnm;// 屬性對象集合
    String attrname;
    String attrval;
    for (int i = ; i < nlgetLength(); i++) {
      e = (Element) em(i);
      Systemoutprintln(egetTagName() + :
      + egetFirstChild()getNodeValue());
      nnm = egetAttributes();
      if (nnm != null) {
      for (int j = ; j < nnmgetLength(); j++) {
      att = (Attr) em(j);
      attrname = attgetName();
      attrval = attgetValue();
      Systemoutprintln(屬性是 + attrname + = + attrval);
      }
      }
    }
    }
    /*
        * 此為添加方法

 */
    public static void addElement(Document dc) {
    Element estu = dccreateElement(student);
    Element ename = dccreateElement(name);
    Element esex = dccreateElement(sex);
    Element eage = dccreateElement(age);
    Element escore = dccreateElement(score);
    Text tname = dccreateTextNode(張三);
    Text tsex = dccreateTextNode();
    Text tage = dccreateTextNode();
    Text tscore = dccreateTextNode();
    estuappendChild(ename)appendChild(tname);
    estuappendChild(esex)appendChild(tsex);
    estuappendChild(eage)appendChild(tage);
    estuappendChild(escore)appendChild(tscore);
    dcgetDocumentElement()appendChild(estu);
    }
    /*
        * 此為修改方法

     */
    public static void updateElement(Document dc String stname String stscore) {
    NodeList nl = dcgetElementsByTagName(name);
    Element e;
    for (int i = ; i < nlgetLength(); i++) {
      e = (Element) em(i);
      if (egetFirstChild()getNodeValue()equals(stname)) {
      Node n = egetParentNode();
      NodeList nd = ngetChildNodes();
      for (int j = ; j < ndgetLength(); j++) {
      Node node = em(j);
      if (nodegetNodeName()equals(score)) {
        nodegetFirstChild()setNodeValue(stscore);
      }
      }
      }
    }
    }
    /*



    * 此方法為刪除方法

     */
    public static void delElement(Document dc String idValue) {
    NodeList nl=dcgetElementsByTagName(student);
    Element e;
    NamedNodeMap nnm;//屬性集合
    Attr att;
    for(int i=;i<nlgetLength();i++){
      e=(Element)em(i);
      nnm=egetAttributes();
      if(nnm!=null){
      for(int j=;j<nnmgetLength();j++){
      att=(Attr)em(j);
      if((attgetName()equals(id))&&(attgetValue()equals(idValue))){
        (egetParentNode())removeChild(e);
      }
      }
      }
    }
    }
    /*

    * 此方法把在程序中所做的操作保存於xml文檔中

     */
    public static void saveXml(Document dom) {
    TransformerFactory tff = TransformerFactorynewInstance();
    try {
      Transformer tf = tffnewTransformer();
      tfsetOutputProperty(encoding utf);
      tftransform(new DOMSource(dom) new StreamResult(
      E:\\xml\\xmlExample\\studentxml));
    } catch (Exception e) {
      eprintStackTrace();
    }
    }
    /**

    * main方法

     */
    public static void main(String[] args) {
    Document dc = getInstance(E:\\xml\\xmlExample\\studentxml);
    // printAll(dc);
    // addElement(dc);
    //updateElement(dc aa );
    delElement(dc);
    saveXml(dc);
    printAttr(dc);
    }
    }


From:http://tw.wingwit.com/Article/program/net/201311/11853.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.