jdom操作xml

本文介绍如何使用JDome库解析和创建XML文件。包括通过SAXBuilder加载XML文档、获取根节点及子节点,并读取其内容和属性;同时演示了如何创建包含元素、属性和文本的XML结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jdom操作xml:
解析过程:
          //创建解析器
           SAXBuilder buil=new SAXBuilder();
           //加载xml文件,返回的是Document对象
           Document doc;
           try {
                doc = buil.build(new FileReader(new File("myfile.xml")));
                //获取xml根节点
                Element root=doc.getRootElement();
                //获取子节点
                List <Element> list=root.getChildren("bookInfo", root.getNamespace());
                for(Element e:list){
                     Element t=e.getChild("title", e.getNamespace());
                     Element a=e.getChild("author", e.getNamespace());
                     String bookName=t.getText();
                     String author=a.getText();
                     int age=Integer.parseInt(a.getAttributeValue("age"));
                     String sex=a.getAttributeValue("sex");
                     System.out.println("书名:"+bookName+ "  作者名:  "+author+"   age:  "+age+"   sex:  "+sex);
                }
           
           } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
           } catch (JDOMException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
           } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
           }
     }


创建XML过程:
           //所有XML内存的根
           Document document=null;
           //创建<myfille></myfile>
           Element root=new Element("myfile");
           Element title=new Element("tile");
           Element author=new Element("author");
           
           //设置author的属性两种方法  第一种方法
           author.setAttribute("age","11");
           //第二种方法(使用对象的方式存入)
           Attribute sex=new Attribute("sex", "22");
           author.setAttribute(sex);
           
           title.setAttribute("name","title");
           
           root.addContent(title);
           root.addContent(author);
           
           //放入文本的两种方式
           author.setText("jack");
           author.addContent("jack");
           title.addContent("java编程思想");
           
           document=new Document(root);
           //对文档单行getCompactFormat();或者多行.getPrettyFormat()显示的确定  
           Format f=Format.getPrettyFormat();
           //必须用GB2312
           f.setEncoding("GB2312");
           //使用jdom封装好的进行开流
           XMLOutputter out=new XMLOutputter(f);
           try {
                out.output(document, new FileWriter(new File("myfile.xml")));
                
           } catch (IOException e) {
                e.printStackTrace();
           }
           System.out.println("end");
           }
           */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值