使用Java的JDOM解析xml代码
2017-03-26 20:38
513 查看
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)+"本书====");
}
}
}
相关文章推荐
- Java XML解析工具 JDOM介绍及使用实例
- 在java中使用dom4j解析xml(示例代码)
- 在java中使用dom4j解析xml(示例代码)
- Java&Xml教程(六)使用JDOM解析XML文件
- 在java中使用dom4j解析xml(示例代码)
- Java&Xml教程(六)使用JDOM解析XML文件
- java使用jdom架包解析xml字符串dom原理并保存map对象
- 在java中使用dom4j解析xml(示例代码)
- Java&Xml教程(六)使用JDOM解析XML文件
- Java使用JDOM解析XML(转载,简单又详细)
- JAVA代码-使用DOM4J解析复杂XML文件
- 在java中使用dom4j解析xml(示例代码)
- Java:XML篇,使用JDOM读取并解析XML
- [JAVA100例]070、使用JDOM解析XML
- 使用dom4j和jdom解析与生成字符串型XML的代码示例
- Java使用Jdom读取xml解析实例
- 在java中使用dom4j解析xml(示例代码)
- 使用dom4j和jdom解析与生成字符串型XML的代码示例
- JAVA 使用Dom4j 解析XML