Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 286|回复: 0

[xml学习]用JDOM遍历xml文档

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-10 04:06:21 | 显示全部楼层 |阅读模式
    Element类的getContent()方法返回一个List对象,它包括了一个元素的所有内容:注释、属性、处理指令、文本和子元素。利用它我们可以遍历XML文档。下面的程序来自《java语言与xml教程》
    import org.jdom.*;
    import org.jdom.input.SAXBuilder;
    import java.io.IOException;
    import java.util.*;

      

      
      public class TreePrinter{
        public static void process(Element element){
           inspect(element);
           List content=element.getContent();//取元素的所有内容
           Iterator iterator=content.iterator();
           while(iterator.hasNext()){
              Object o=iterator.next();
              if(o instanceof Element){//如果是子元素
                  Element child=(Element)o;
                  process(child);//递归调用
              }else if(o instanceof Comment){//如果是说明
                  Comment c=(Comment)o;
                  //System.out.println(c.getText());
                 // System.out.println();
           }
        }

        public static void inspect(Element element){
             if(!element.isRootElement()){
                System.out.println();
             }  
      
       
       
         
       

       
       
      

             String qualifiedName=element.getQualifiedName();
             System.out.println(qualifiedName+":"+element.getText());
             Namespace namespace=element.getNamespace();
             if(namespace!=Namespace.NO_NAMESPACE){
                  String localName=element.getName();
                  String uri=element.getNamespaceURI();
                  String prefix=element.getNamespacePrefix();
                  System.out.println("  Local name: "+localName);
                  System.out.println("  Namespace URI: "+uri);
                  if(!"".equals(prefix)){
                        System.out.println("  Namespace prefix: "+prefix);
                  }
              }

            List attributes=element.getAttributes();
            if(!attributes.isEmpty()){
                 Iterator iterator=attributes.iterator();
                 while(iterator.hasNext()){
                    Attribute attribute=(Attribute)iterator.next();
                    String name=attribute.getName();
                    String value=attribute.getValue();
                    Namespace attributeNamespace=attribute.getNamespace();
                    if(attributeNamespace==Namespace.NO_NAMESPACE){
                      System.out.println("  "+name+ "=""+value+ """);
                    }else{
                      String prefix=attributeNamespace.getPrefix();
                      System.out.println("  "+prefix+":"+name+"=""+value+""");
                    }
                  }
              }

              List namespaces=element.getAdditionalNamespaces();
              if(!namespaces.isEmpty()){
                 Iterator iterator=namespaces.iterator();
                 while(iterator.hasNext()){
                     Namespace additional=(Namespace)iterator.next();
                     String uri=additional.getURI();
                     String prefix=additional.getPrefix();
                     System.out.println("  xmlns:"+prefix+"=""+uri+""");
                  }
               }
         }

            public static void main(String[] args){
              if(args.length<=0){
                 System.out.println("Usage: java TreePrinter URL");
                 return;
               }
              String url=args[0];
              try{
                   SAXBuilder parser=new SAXBuilder();
                   Document document=parser.build(url);
                   process(document.getRootElement());
             }catch(JDOMException e){
                   System.out.println(url+" is not well-formed.");
             }catch(IOException e){
                   System.out.println("Due to an IOException,the parser could not encode "+url);
             }
           }
      }

        运行结果:

    C:java>java   TreePrinter sample.xml
    HD:
       xmlns:dd="http://www.zzjava.hnzz.net"
       xmlns:ss="http://www.hnzz.net"
    disk:
       name="C"
    capacity:8G
    directories:200
    files:1580
    disk:
       name="D"
    capacity:10G
    directories:500
    files:3000

    用的xml文件是:
    <?xml version="1.0" encoding="UTF-8"?>
    <HD xmlns:dd="http://www.zzjava.hnzz.net"  xmlns:ss="http://www.hnzz.net" >
      

      
       
             <disk name="C">
                     <capacity>8G</capacity>
                     <directories>200</directories>
                     <files>1580</files>
             </disk>

             <disk name="D">
                     <capacity>10G <!--good-->
                     </capacity>
                     <directories>500</directories>
                     <files>3000</files>
             </disk>
    </HD>  
      

       
       
       

       
      
      


      

      



                            function TempSave(ElementID)
                            {
                                    CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value);
                                    CommentsPersistDiv.save("CommentXMLStore");
                            }
                            function Restore(ElementID)
                            {
                                    CommentsPersistDiv.load("CommentXMLStore");
                                    document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent");
                            }
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-23 01:52 , Processed in 0.366068 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表