您的位置:首页 > 编程语言 > Java开发

java xml jdom创建 修改 解析

2017-04-03 10:35 417 查看


注:需要jdom的jar包

package dom.parse;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import org.jdom.Attribute;

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 JDomTest {

 public static void main(String[] args) throws FileNotFoundException, IOException, JDOMException {

  //createJDomXml();

  String path="src/dom/parse/jdom.xml";

  //modifyJDomXml(path);

  parseXml(path);

  

 }

 

 

 private static void parseXml(String path) throws JDOMException, IOException {

   SAXBuilder builder = new SAXBuilder();

         Document doc = builder.build(path);

         Element root = doc.getRootElement();

         @SuppressWarnings("unchecked")

   List<Element> elementlist = root.getChildren();

         List<Employee1> employee1List = new ArrayList<Employee1>();

       

         for (int i = 0; i < elementlist.size(); i++) {

           Employee1 employee1 = new Employee1();

          Element e = elementlist.get(i);

          @SuppressWarnings("unchecked")

    List<Attribute> attributeList = e.getAttributes();

          String employeeId=null;

          String employeeName=null;

               

                 for (int j = 0; j < attributeList.size(); j++) {

               if(j==0){

                employeeId=attributeList.get(j).getValue();

               }

               if(j==1){

                employeeName=attributeList.get(j).getValue();

               }

         

        }

             

          

          employee1.setEmployeeId(employeeId);

          employee1.setEmployeeName(employeeName);

          employee1.setContent(e.getValue());

          List<Jiangli> jingliList = new ArrayList<Jiangli>();

          Jiangli jiangli= new Jiangli();

          

          

          if(null != e.getChildren())

          {

           List<Element> childrenList = e.getChildren();

           

           for (int j = 0; j < childrenList.size(); j++) {

            Element child = childrenList.get(j);

            //System.out.println(child);

               List<Attribute> attributeChildrenList = child.getAttributes();

               Attribute childrenAttribute=null;

               String jiangliName=null;

               String year=null;

               for (int k = 0; k < attributeChildrenList.size(); k++) {

                if(k==0)

                {

                  jiangliName=attributeChildrenList.get(k).getValue();

                }

                if(k==1)

                {

                  year=attributeChildrenList.get(k).getValue();

                }

      }

               jiangli.setJiangliName(jiangliName);

               jiangli.setYear(year);

               jiangli.setContent(child.getValue());

               //System.out.println(jiangli);

               jingliList.add(jiangli);

     }

           

           

          }

          

          employee1.setJianglis(jingliList);

          //employee1List.add(employee1);

          System.out.println(employee1);

          //System.out.println(employee1List);

   }

        

        

 }

 private static void modifyJDomXml(String path) throws JDOMException, IOException {

        SAXBuilder builder = new SAXBuilder();

        Document doc = builder.build(path);

       

        Element root = doc.getRootElement();

        System.out.println(root);

        System.out.println(root.getName());

       

       /* Element employee1= root.getChild("employee");

       

        List list=employee1.getAttributes();

        for (int i = 0; i < list.size(); i++) {

   Attribute attr = (Attribute) list.get(i);

      System.out.println(attr.getName()+"--->"+attr.getValue());

  }*/

       

       

        List<Element> list = root.getChildren();

        for (int i = 0; i < list.size(); i++) {

         

         Element e = list.get(i);

         System.out.println(e.getName()+"---->"+e.getValue());

         if (i==2) {

          Element zi=list.get(i).getChild("jiangli");

          System.out.println("    "+zi.getName()+"---->"+zi.getValue());

          e.removeChild(zi.getName());

   }

   

  }

       

        XMLOutputter output = new XMLOutputter(Format.getPrettyFormat());

        output.output(doc,new FileOutputStream("src/dom/parse/jdom2.xml") );

       

 }

 private static void createJDomXml() throws FileNotFoundException, IOException {

  Document document = new Document();

  Element root = new Element("employees");

  document.addContent(root);

  

  Element e1 = new Element("employee");

  e1.setAttribute("employeeId","1");

  e1.setAttribute("employeeName","zhangsan");

  e1.setText("xianjiaoda");

  root.addContent(e1);

  

  Element e2 = new Element("employee");

  Attribute attr = new Attribute("employeeId","2");

  e2.setAttribute(attr);

  root.addContent(e2);

  

  

  Element e3 = new Element("employee");

  e3.setAttribute("employeeId","3").setAttribute("name","wangwu");

  e3.addContent(new Element("jiangli").setAttribute("sanhaoxuesheng","2012").setAttribute("sanhaobiaobing","2013").setText("xianjiaoda"));

  root.addContent(e3);

  

  Format format = Format.getPrettyFormat();

  format.setIndent("    ");

  //format.setEncoding("gbk");

  

  

  XMLOutputter output = new XMLOutputter(format);

  output.output(document,new FileOutputStream("src/dom/parse/jdom.xml"));  

 }

 

}

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: