Jdom操作、创建和解析XML

jopen 10年前

 需要导入Jdom的一个jar包;    1、Jdom创建XML      import java.io.FileWriter;    import org.jdom.Attribute;  import org.jdom.Comment;  import org.jdom.Document;  import org.jdom.Element;  import org.jdom.output.Format;  import org.jdom.output.XMLOutputter;    public class JDomTest1  {   public static void main(String[] args) throws Exception   {          //创建文档    Document document = new Document();          //创建文档根元素    Element root = new Element("root");          //向文档呢添加根元素    document.addContent(root);    //添加注释    Comment comment = new Comment("This is my comments");          //在根元素下添加注释    root.addContent(comment);          //创建一个元素    Element e = new Element("hello");          //给元素添加属性的第一种方式,它有返回值,返回一个Element对象    e.setAttribute("sohu", "www.sohu.com");          //给根元素添加属性    root.addContent(e);      Element e2 = new Element("world");          //创建元素属性的第二种方式    Attribute attr = new Attribute("test", "hehe");          //给元素添加属性    e2.setAttribute(attr);          //将元素添加到根元素下    e.addContent(e2);          //添加元素属性和文本内容,属于方法链的编程风格    e2.addContent(new Element("aaa").setAttribute("a", "b")      .setAttribute("x", "y").setAttribute("gg", "hh").setText("text content"));      //设置其格式,一般还有一个没有缩进与空格的格式getRawFormat,主要用于XML的网络传输,因为它没有空格,减少网络传输的数据量。    Format format = Format.getPrettyFormat();        //设置元素前面空格    format.setIndent("    ");    //设置编码方式  //  format.setEncoding("gbk");    //将XML文档输出    XMLOutputter out = new XMLOutputter(format);    out.output(document, new FileWriter("jdom.xml"));     }  }         2、Jdom解析XML      import java.io.File;  import java.io.FileOutputStream;  import java.util.List;    import org.jdom.Attribute;  import org.jdom.Document;  import org.jdom.Element;  import org.jdom.input.SAXBuilder;  import org.jdom.output.Format;  import org.jdom.output.XMLOutputter;    public class JDomTest2  {   public static void main(String[] args) throws Exception   {    //JDOM从XML中解析    SAXBuilder builder = new SAXBuilder();    //获得XML文档对象    Document doc = builder.build(new File("jdom.xml"));    //得到文档根元素    Element element = doc.getRootElement();        System.out.println(element.getName());    //得到根元素下的hello元素    Element hello = element.getChild("hello");        System.out.println(hello.getText());    //得到hello子元素下的属性    List list = hello.getAttributes();        //得到hello元素下属性的名字和值    for(int i = 0 ;i < list.size(); i++)    {     Attribute attr = (Attribute)list.get(i);          String attrName = attr.getName();     String attrValue = attr.getValue();          System.out.println(attrName + "=" + attrValue);    }    //删除hello下的world子元素    hello.removeChild("world");        //将文档保存到另一个文件    XMLOutputter out = new XMLOutputter(Format.getPrettyFormat().setIndent("    "));        out.output(doc, new FileOutputStream("jdom2.xml"));         }  }         3、综合实例      import java.io.File;  import java.io.FileNotFoundException;  import java.io.FileOutputStream;  import java.io.IOException;  import java.util.List;    import org.jdom.Document;  import org.jdom.Element;  import org.jdom.JDOMException;  import org.jdom.input.SAXBuilder;  import org.jdom.output.Format;  import org.jdom.output.XMLOutputter;    public class XMLTest  {   public static void main(String[] args)   {    modifyXML();   }     public static void createXML()   {    Element root = new Element("学生信息");    Element stu1 = new Element("学生");    Element stu2 = new Element("学生");    Element name1 = new Element("姓名");    Element sex1 = new Element("性别");    Element age1 = new Element("年龄");    Element name2 = new Element("姓名");    Element sex2 = new Element("性别");    Element age2 = new Element("年龄");      name1.setText("小明");    sex1.setText("男");    age1.setText("19");      name2.setText("小花");    sex2.setText("女");    age2.setText("20");      root.addContent(stu1);    root.addContent(stu2);    stu1.addContent(name1);    stu1.addContent(sex1);    stu1.addContent(age1);    stu2.addContent(name2);    stu2.addContent(sex2);    stu2.addContent(age2);      Document doc = new Document(root);    Format f = Format.getPrettyFormat();    f.setEncoding("gb2312");    XMLOutputter out = new XMLOutputter(f);      try    {     out.output(doc, new FileOutputStream(new File("c:/myxml.xml")));    }    catch (FileNotFoundException e)    {     e.printStackTrace();    }    catch (IOException e)    {     e.printStackTrace();    }     }     public static void searchXML()   {    SAXBuilder build = new SAXBuilder();    try    {     Document doc = build.build("c:/myxml.xml");     Element e = doc.getRootElement();       List<Element> list = e.getChildren();     for (Element ele : list)     {      System.out.println(ele.getChild("姓名").getName() + ":"        + ele.getChild("姓名").getText());      System.out.println(ele.getChild("性别").getName() + ":"        + ele.getChild("性别").getText());      System.out.println(ele.getChild("年龄").getName() + ":"        + ele.getChild("年龄").getText());     }    }    catch (JDOMException e)    {     e.printStackTrace();    }    catch (IOException e)    {     e.printStackTrace();    }   }      public static void addElement()   {    SAXBuilder build=new SAXBuilder();    try    {     Document doc=build.build("c:/myxml.xml");     Element root=doc.getRootElement();     Element stu=root.getChild("学生");     Element cla=new Element("班级");     cla.addContent("2班");     stu.addContent(cla);     XMLOutputter out=new XMLOutputter();     out.output(doc,new FileOutputStream(new File("c:/myxml.xml")));            }    catch (JDOMException e)    {     e.printStackTrace();    }    catch (IOException e)    {     e.printStackTrace();    }       }      public static void modifyXML()   {    SAXBuilder build=new SAXBuilder();    Document doc;    try    {     doc = build.build("c:/myxml.xml");     Element root=doc.getRootElement();     Element stu=root.getChild("学生");     Element age=stu.getChild("年龄2");        age.setName("年龄");     age.setText("10");     List<Element> list=stu.getChildren();         for(Element e:list)     {      if(e.getName().equals(""));  //    {  ////     Element height=new Element("身高");  ////     height.setText("175厘米");  ////     root.addContent(height);  //         stu.removeChild(str);  //    }     }     XMLOutputter out=new XMLOutputter();     out.output(doc,new FileOutputStream(new File("c:/myxml.xml")));    }    catch (JDOMException e)    {     e.printStackTrace();    }    catch (IOException e)    {     e.printStackTrace();    }   }    }