-
=======開始遍歷某一本書======== 節點名:id---節點值:1 =======結束遍歷某一本書======== =======開始遍歷某一本書======== 節點名:id---節點值:2 =======結束遍歷某一本書========查看全部
-
package com.imooc.dom4jtest; import java.io.File; import java.util.Iterator; import java.util.List; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class DOM4JTest { public static void main(String[] args) { // 解析books.xml文件 // 創建SAXReader的對象reader SAXReader reader = new SAXReader();查看全部
-
try { // 通過reader對象的read方法加載books.xml文件,獲取document對象 Document document = reader.read(new File("src/res/books.xml")); // 通過document對象獲取根節點bookstore Element bookStore = document.getRootElement(); // 通過element對象的elementIterator()方法獲取迭代器 Iterator it = bookStore.elementIterator(); // 遍歷迭代器,獲取根節點中的信息(書籍) while (it.hasNext()) { Element book = (Element) it.next(); // 獲取book的屬性名以及屬性值 List<Attribute> bookAttrs = book.attributes(); for (Attribute attr : bookAttrs) { System.out.println("節點名:" + attr.getName() + "---節點值:" + attr.getValue()); } } } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 運行結果: 節點名:id---節點值:1 節點名:id---節點值:2查看全部
-
DOM優點缺點解析查看全部
-
這一節最簡單查看全部
-
***********運行結果貌似不符********** ====開始解析第1書==== 屬性名:id---屬性值:1 節點名:name節點值:冰與火之歌 節點名:author節點值:喬治馬丁 節點名:year節點值:2014 節點名:price節點值:89 ====結束解析第1書==== 1 1 name ====開始解析第2書==== 屬性名:id---屬性值:2 節點名:name節點值:安徒生童話 節點名:year節點值:2004 節點名:price節點值:77 節點名:language節點值:English ====結束解析第2書==== 2 1 name查看全部
-
if(attrName.equals("id")){ bookEntity.setId(attrValue); } } // 對book節點的子節點的節點名以及節點值的遍歷 List<Element> bookChilds = book.getChildren(); for (Element child : bookChilds) { System.out.println("節點名:" + child.getName() + "節點值:" + child.getValue()); if(child.getName().equals("name")){ bookEntity.setName(child.getName()); } else if(child.getName().equals("author")){ bookEntity.setAuthor(child.getValue()); } else if(child.getName().equals("year")){ bookEntity.setYear(child.getValue()); } else if(child.getName().equals("price")){ bookEntity.setPrice(child.getValue()); } else if(child.getName().equals("language")){ bookEntity.setLanguage(child.getValue()); } }查看全部
-
package com.imooc.entity; public class Book { private String id; private String name; private String author; private String year; private String price; private String language; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getYear() { return year; } public void setYear(String year) { this.year = year; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public String getLanguage() { return language; } public void setLanguage(String language) { this.language = language; } }查看全部
-
<?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book id="1"> <name>?°?????1?-</name> <author>?1?2?é????</author> <year>2014</year> <price>89</price> </book> <book id="2"> <name>???????¥èˉ</name> <year>2004</year> <price>77</price> <language>English</language> </book> </bookstore> in = new FileInputStream("src/res/books.xml"); InputStreamReader isr = new InputStreamReader(in,"UTF-8"); // 3.通過saxBuilder的build方法將輸入流加載到saxBuilder中 Document document = saxBuilder.build(isr); 運行結果 ====開始解析第1書==== 屬性名id---屬性值1 節點名name節點值冰與火之歌 節點名author節點值喬治馬丁 節點名year節點值2014 節點名price節點值89 ====結束解析第1書==== ====開始解析第2書==== 屬性名id---屬性值2 節點名name節點值安徒生童話 節點名year節點值2004 節點名price節點值77 節點名language節點值English ====結束解析第2書====查看全部
-
Java解析XML文件有四種解析方式: DOM SAX DOM4J JDOM查看全部
-
JDOM解析亂碼處理 1.修改xml第一行的encoding屬性 2.在代碼中處理,應用IO流知識 //創建輸入流,將XML文件加載到輸入流中 InputStream in = new FileInputStream("src/book.xml"); //使用包裝流InputStreamReader進行讀取編碼的指定 InputStreamReader isr = new InputStreamReader(in,"UTF-8");查看全部
-
<?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book id="1"> <name>?°?????1?-</name> <author>?1?2?é????</author> <year>2014</year> <price>89</price> </book> <book id="2"> <name>???????¥èˉ</name> <year>2004</year> <price>77</price> <language>English</language> </book> </bookstore> 運行結果 ====開始解析第1書==== 屬性名id---屬性值1 節點名name節點值?°?????1?- 節點名author節點值?1?2?é???? 節點名year節點值2014 節點名price節點值89 ====結束解析第1書==== ====開始解析第2書==== 屬性名id---屬性值2 節點名name節點值???????¥èˉ 節點名year節點值2004 節點名price節點值77 節點名language節點值English ====結束解析第2書====查看全部
-
// 對book節點的子節點的節點名以及節點值的遍歷 List<Element> bookChilds = book.getChildren(); for (Element child : bookChilds) { System.out.println("節點名:" + child.getName() + "節點值:" + child.getValue()); } 運行結果: ====開始解析第1書==== 屬性名:id---屬性值:1 節點名:name節點值:冰與火之歌 節點名:author節點值:喬治馬丁 節點名:year節點值:2014 節點名:price節點值:89 ====結束解析第1書==== ====開始解析第2書==== 屬性名:id---屬性值:2 節點名:name節點值:安徒生童話 節點名:year節點值:2004 節點名:price節點值:77 節點名:language節點值:English ====結束解析第2書====查看全部
-
List<Element> bookList= rootElement.getChildren(); //繼續進行解析 // bookList.size(); // 也可以用for()循環 for (Element book : bookList) { System.out.println("====開始解析第" + (bookList.indexOf(book)+1)+"書===="); //解析book的屬性 List<Attribute> attrList =book.getAttributes(); // book.getAttribute(""); // //知道節點下屬性名稱時,獲取節點值 // book.getAttributeValue("id"); //遍歷attrList(針對不清楚book節點下屬性的名字及數量) for (Attribute attr : attrList) { //獲取屬性名 String attrName = attr.getName(); //獲取屬性值 String attrValue = attr.getValue(); System.out.println("屬性名:" + attrName + "---屬性值:" + attrValue); } System.out.println("====結束解析第" + (bookList.indexOf(book)+1)+"書===="); }查看全部
-
for (Element book : bookList) { System.out.println("====開始解析第" + (bookList.indexOf(book)+1)+"書===="); //解析book的屬性 List<Attribute> attrList =book.getAttributes(); //遍歷attrList for (Attribute attr : attrList) { //獲取屬性名 String attrName = attr.getName(); //獲取屬性值 String attrValue = attr.getValue(); System.out.println("屬性名:" + attrName + "---屬性值:" + attrValue); } System.out.println("====結束解析第" + (bookList.indexOf(book)+1)+"書===="); } 運行結果: ====開始解析第1書==== 屬性名:id---屬性值:1 ====結束解析第1書==== ====開始解析第2書==== 屬性名:id---屬性值:2 ====結束解析第2書====查看全部
舉報
0/150
提交
取消