遍历xml删选_用JDOM遍历xml文档

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文件是:

8G

200

1580

10G 

500

3000

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 船长 阅读(1342) 评论(1)  编辑  收藏 所属分类: J2EE

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值