使用Java的JDOM解析xml代码
来源:互联网 发布:win10网络凭证 编辑:程序博客网 时间:2024/05/16 14:43
package com.imooc.learn3;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
public class JDomTest {
public static void main(String[] args) throws JDOMException, IOException {
//对Books.xml进行解析
SAXBuilder saxBuilder=new SAXBuilder();
//创建一个输入流
FileInputStream in=new FileInputStream("demo/Books.xml");
//通过SaxBuilder来处理文件输入流
Document document=saxBuilder.build(in);
//通过document对象来获取xml的根节点
Element root=document.getRootElement();
//通过getChildren来获取根节点下面子节点的集合
List<Element> aList=root.getChildren();
//通过For循环来遍历
for (Element element : aList) {
System.out.println("====开始解析第"+(aList.indexOf(element)+1)+"本书====");
System.out.println(element.getName()+"----");
List<Attribute> attrList=element.getAttributes();
//遍历attrList,对于不知道节点的属性名和值
for (Attribute attribute : attrList) {
System.out.println("属性名是:"+attribute.getName());
System.out.println("该属性值是"+attribute.getValue());
}
//针对book的子节点的节点名和节点值进行遍历
List<Element> chiList=element.getChildren();
for (Element elechild : chiList) {
System.out.println("节点名字是:"+elechild.getName()+"节点值是:"+elechild.getValue());
// System.out.println();
}
System.out.println("====结束解析第"+(aList.indexOf(element)+1)+"本书====");
}
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
public class JDomTest {
public static void main(String[] args) throws JDOMException, IOException {
//对Books.xml进行解析
SAXBuilder saxBuilder=new SAXBuilder();
//创建一个输入流
FileInputStream in=new FileInputStream("demo/Books.xml");
//通过SaxBuilder来处理文件输入流
Document document=saxBuilder.build(in);
//通过document对象来获取xml的根节点
Element root=document.getRootElement();
//通过getChildren来获取根节点下面子节点的集合
List<Element> aList=root.getChildren();
//通过For循环来遍历
for (Element element : aList) {
System.out.println("====开始解析第"+(aList.indexOf(element)+1)+"本书====");
System.out.println(element.getName()+"----");
List<Attribute> attrList=element.getAttributes();
//遍历attrList,对于不知道节点的属性名和值
for (Attribute attribute : attrList) {
System.out.println("属性名是:"+attribute.getName());
System.out.println("该属性值是"+attribute.getValue());
}
//针对book的子节点的节点名和节点值进行遍历
List<Element> chiList=element.getChildren();
for (Element elechild : chiList) {
System.out.println("节点名字是:"+elechild.getName()+"节点值是:"+elechild.getValue());
// System.out.println();
}
System.out.println("====结束解析第"+(aList.indexOf(element)+1)+"本书====");
}
}
}
0 0
- 使用Java的JDOM解析xml代码
- java解析xml之二:使用JDOM
- java解析xml之二:使用JDOM
- 使用Jdom解析XML
- 使用JDOM解析XML
- 使用JDOM解析XML
- 使用jdom解析xml
- 使用JDOM解析XML
- 使用JDOM解析XML
- 使用JDOM解析XML
- 使用JDOM解析XML
- 使用JDOM解析xml
- 使用Jdom解析XML
- java jdom 解析xml
- JAVA jdom 解析 xml
- JAVA JDom解析XML
- java jdom解析xml
- java jdom 解析xml
- 网易2017年实习生NLP岗编程题--赶去公司
- 第一次发表
- 【POJ2406】Power Strings(KMP与最小循环节的故事)
- memcached 可视化客户端工具TreeNMS
- C++第三次实验-字符串
- 使用Java的JDOM解析xml代码
- 关于鼠标的3个相关监听事件
- APM/PIXhawk 资料总汇(欢迎补充更新)
- ASCII码排序
- 数据分析
- Imageloder缓存机制,缓存到data下面,并且删除
- 指针进阶
- h5表单的标题和属性
- 关于NP问题与P问题的认识