getchild("list")
返回个List
然后遍历List
用getname()判断元素名是不是id

解决方案 »

  1.   

    如何实现往一个XML文件中写内容,当文件(这个XML)存在的时候进行结尾追加内容?
      

  2.   

    public List readXML(String fn){
        Document doc = null;
        File file = new File(fn);
        if(file.exists()){
          SAXBuilder sb = new SAXBuilder();
          try{
            doc = sb.build(file);
          }catch(JDOMException e){
            System.out.println("异常:"+e.getMessage());
          }catch(IOException e){
            System.out.println(e.getMessage());
          }
          List return_list = new ArrayList();//定义返回对象
          Element root = doc.getRootElement();//获得根节点,即一级元素
          List list_two = root.getChildren();//获得根节点下级元素列表,即二级元素
          //List child_list=doc.getRootElement().getChildren("data-source");//获得指定元素下的元素列表
          for(int i=0;i<list_two.size();i++){
            Element el_two = (Element)list_two.get(i);//类型转换
            List list_three = el_two.getChildren();//获得三级元素
            //System.out.println(list_two.get(i));
            //System.out.println("性别:"+el_two.getAttribute("性别").getValue());//获得属性值
            for(int j=0;j<list_three.size();j++){
              Hashtable ht = new Hashtable();//存放数据
              Element el_three = (Element)list_three.get(j);//类型转换
              String el_three_text = el_three.getValue();//获得元素的内容
              //System.out.println(el_three.getName()+"----"+el_three_text);
              ht.put(el_three.getName(),el_three_text);
              return_list.add(ht);
            }
          }
          //Enumeration key = ht.keys();
          return return_list;
        }else{
          System.out.println("指定文件不存在!");
          return null;
        }
      }上面是我们在程序中用到的一部分,楼主可以参考着修改一下就可以了!
      

  3.   

    package com.xml;import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.JDOMException;
    import org.jdom.input.*;
    import org.jdom.output.*;
    import org.jdom.output.Format;
    import org.jdom.*;
    import java.util.*;
    import java.io.*;public class HelloXML {
      public HelloXML() {
      }
      /**
       * 插入一个新的元素
       * @param filename
       * @param id 判断是否有重复记录
       * 需要改进:添加一个数组类型的参数,用数组存放记录的值,然后通过Element的setText()方法来设置值
       */
      public void EditXML(String filename,String id){
        //检测文件中是否存在相同的记录,通过id来进行判断
        if(this.CheckId(filename,id)){
          File fn = new File(filename);
          Document doc = null;
          SAXBuilder sb = new SAXBuilder();
          if (fn.exists()) {
            try {
              doc = sb.build(new FileInputStream(filename));
            }
            catch (Exception e) {
              System.out.println(e.getMessage());
            }
            Element root = doc.getRootElement();//或得根级节点
              Element el_three = new Element("list");//新建二级节点
              //记录
              Element el_id = new Element("id");
              el_id.setText("6");
              Element el_username = new Element("username");
              el_username.setText("4");
              Element el_time = new Element("time");
              el_time.setText("4");
              Element el_homepage = new Element("hompage");
              el_homepage.setText("4");
              Element el_email = new Element("email");
              el_email.setText("4");
              Element el_context = new Element("context");
              el_context.setText("4");
              //记录内容
              el_three.addContent(el_id);
              el_three.addContent(el_username);
              el_three.addContent(el_time);
              el_three.addContent(el_homepage);
              el_three.addContent(el_email);
              el_three.addContent(el_context);
              //
              root.addContent(el_three);
              //
              //设置输出格式
              Format f = Format.getRawFormat();
              f.setIndent("");
              f.setEncoding("GBK");
              XMLOutputter output = new XMLOutputter(f);
              //XMLOutputter output = new XMLOutputter(" ",true,"GBK");
              try {
                output.output(doc, new FileOutputStream(filename));
              }
              catch (Exception e) {
                System.out.println(e.getMessage());
              }
          }
          else {
            System.out.println("找不到指定的文件!");
          }
        }else{
          System.out.println("该条记录已经存在");
        }
      }
      public boolean CheckId(String fn,String id){
        Document doc = null;
        File file = new File(fn);
        if(file.exists()){
          SAXBuilder sb = new SAXBuilder();
          try{
            doc = sb.build(file);
          }catch(JDOMException e){
            System.out.println("异常:"+e.getMessage());
          }catch(IOException e){
            System.out.println(e.getMessage());
          }
          List return_list = new ArrayList();//定义返回对象
          Element root = doc.getRootElement();//获得根节点,即一级元素
          List list_two = root.getChildren();//获得根节点下级元素列表,即二级元素
          for(int i=0;i<list_two.size();i++){
            Element el_two = (Element)list_two.get(i);//类型转换
            List list_three = el_two.getChildren("id");//获得元素名为id的list对象
            for(int j=0;j<list_three.size();j++){
              Hashtable ht = new Hashtable();//存放数据
              Element el_three = (Element)list_three.get(j);//类型转换
              String el_three_text = el_three.getValue();//获得元素的内容
              //System.out.println(el_three.getName()+"----"+el_three_text);
              if(el_three_text.equals(id)){
                return false;//说明有重复ID
              }
            }
          }
        }
        return true;
      }
      public static void main(String[] args) {
        HelloXML helloXml = new HelloXML();
        System.out.println(helloXml.CheckId("D:\\project\\XML\\project\\project\\test.xml","4"));
        helloXml.EditXML("D:\\project\\XML\\project\\project\\test.xml","6");
        //System.out.println(ok);
      }}test.xml文件内容:<?xml version="1.0" encoding="GBK"?>
    <lists>
     <list>
      <id>1</id>
      <username>冯奎</username>
      <time>01-4-23 18:26:56</time>
      <homepage>http://www.getjob.com.cn</homepage>
      <email>[email protected]</email>
      <context>我的留言</context>
     </list>
     <list>
      <id>2</id>
      <username>妞妞</username>
      <time>01-6-23 18:26:56</time>
      <homepage>http://www.neworiental.org</homepage>
      <email>[email protected]</email>
      <context>测试留言</context>
     </list>
     <list>
      <id>4</id>
      <username>4</username>
      <time>4</time>
      <hompage>4</hompage>
      <email>4</email>
      <context>4</context>
     </list>
    </lists>
      

  4.   

    可以写进去
    但是出现,
    每次增加一个结点,XML文件中第一个结点就如下增加一个黑点
    比如
     <list>
    ...
      <id>1</id>
    ...
      <username>冯奎</username>
    ...
      <time>01-4-23 18:26:56</time>
    ...
      <homepage>http://www.getjob.com.cn</homepage>
    ...
      <email>[email protected]</email>
    ...
      <context>我的留言</context>
    ...
     </list>
    ...
     <list>
    ..
      <id>2</id>
    ..
      <username>妞妞</username>
      <time>01-6-23 18:26:56</time>
      <homepage>http://www.neworiental.org</homepage>
      <email>[email protected]</email>
      <context>测试留言</context>
     </list>
     <list>
    .
      <id>4</id>
      <username>4</username>
      <time>4</time>
      <hompage>4</hompage>
      <email>4</email>
      <context>4</context>
     </list>
    是什么原因呢?