随笔-72  评论-63  文章-0  trackbacks-0
   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>

http://blog.iyi.cn/start/2005/01/javaxml.html
http://toney.cnblogs.com/archive/2004/11/20/66178.aspx
posted on 2006-06-20 15:18 船长 阅读(1488) 评论(1)  编辑  收藏 所属分类: J2EE

评论:
# re: 用JDOM遍历xml文档 2009-05-06 16:54 | shang
疯狂的船长,若是我想读出xml用表格的方式显示出来,比如显示出一份财务报表,应该怎么写?  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: