十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
本篇內(nèi)容介紹了“如何利用Jdom讀取xml”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
新建 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”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!