本篇内容介绍了“如何利用Jdom读取xml”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
郑州ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!
新建 contents.xml
新建 SAXBuilderTest.java
//注意 /jdom.jar;./xerces.jar 加入到classpath中
import java.io.File;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
public class SAXBuilderTest {
private static String titlename;
private String chapter;
private String topic;
public static void main(String[] args) {
try {
SAXBuilder builder = new SAXBuilder();
Document document = builder.build(new File("contents.xml"));
//==============================
Element root = document.getRootElement();
//==============================
Element title = root.getChild("title");
titlename = title.getText();
System.out.println("BookTitle: " + titlename);
//==============================
Element contents = root.getChild("contents");
List chapters = contents.getChildren("chapter");
Iterator it = chapters.iterator();
while (it.hasNext()) {
//==============================
Element chapter = (Element) it.next();
String chaptertitle = chapter.getAttributeValue("title");
System.out.println("ChapterTitle: " + chaptertitle);
List topics = chapter.getChildren("topic");
Iterator iterator = topics.iterator();
while (iterator.hasNext()) {
//==============================
Element topic = (Element) iterator.next();
String topicname = topic.getText();
System.out.println("TopicName: " + topicname);
}
}
} catch (Exception ex) {
}
}
}
“如何利用Jdom读取xml”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!