一、RSS相關內容java
二、解析XML技術apache
三、讀取XML的步驟app
四、Document接口dom
五、Node&Elementiphone
六、查詢事例ui
<?xml version="1.0" encoding="UTF-8"?> <PhoneInfo> <Brand name="華爲"> <Type name="U8650"/> </Brand> <Brand name="蘋果"> <Type name="iphone4"/> <Type name="iphone5"/> </Brand> </PhoneInfo>
package com.ljb.app.xml; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; /** * 用DOM解析XML(查詢、增長、刪除、修改) * @author LJB * @version 2015年3月23日 */ public class DomParseXml { /** * @param args */ public static void main(String[] args) { find(); } /** * 查詢方法 */ public static void find () { try{ // 建立解析器工廠實例 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 從DOM工廠得到解析器 DocumentBuilder db = dbf.newDocumentBuilder(); // 獲取DOM樹 Document doc = db.parse("src/main/java/phone.xml"); // 獲得<Brand>節點信息 NodeList brandList = doc.getElementsByTagName("Brand"); // 循環Brand信息 for (int i = 0 ; i < brandList.getLength() ; i++) { Node brandNode = brandList.item(i); Element brandElement = (Element)brandNode; String brandName = brandElement.getAttribute("name"); NodeList typeList = brandElement.getElementsByTagName("Type"); for (int j = 0 ; j < typeList.getLength() ; j++) { Node typeNode = typeList.item(j); Element typeElement = (Element)typeNode; String typeName = typeElement.getAttribute("name"); System.out.println("手機: " + brandName + typeName); } } } catch (Exception e) { e.printStackTrace(); } } }
執行結果:this
手機: 華爲U8650
手機: 蘋果iphone4
手機: 蘋果iphone5
七、獲取文本節點spa
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE book SYSTEM "test.dtd"> <!-- country 中國 count 印度 rights 版權全部 pricenotation $ type 支票或現金 默認爲現金 --> <book> <details> <name>xml 使用詳解</name> <author>成龍來自&country;</author> <publication>Mac &rights;</publication> <price type="支票">&pricenotation;50</price> </details> <details> <name>xml 揭祕</name> <author>Raghu 來自&count;</author> <publication>Mac &rights;</publication> <price>&pricenotation;45</price> </details> </book>
<!ELEMENT book (details+)> <!ELEMENT details (name,author,publication,price)> <!ELEMENT name (#PCDATA)> <!ELEMENT author (#PCDATA)> <!ELEMENT publication (#PCDATA)> <!ELEMENT price (#PCDATA)> <!ATTLIST price type (支票|現金) "現金" > <!ENTITY country "中國"> <!ENTITY count "印度"> <!ENTITY rights "版權全部"> <!ENTITY pricenotation "$">
package com.ljb.app.xml; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; /** * 用DOM解析XML(查詢、增長、刪除、修改) * @author LJB * @version 2015年3月23日 */ public class DomParseXml { /** * @param args */ public static void main(String[] args) { getTextNode(); } /** * 獲取DOM樹 */ public static Document getDom () { try { // 建立解析器工廠實例 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 從DOM工廠得到解析器 DocumentBuilder db = dbf.newDocumentBuilder(); // 獲取DOM樹 Document doc = db.parse("src/main/java/third.xml"); return doc; } catch (Exception e) { e.printStackTrace(); return null; } } /** * 獲取文本節點 */ public static void getTextNode () { Node priceNode = getDom().getElementsByTagName("price").item(0); Element priceElement = (Element)priceNode; String price = priceElement.getFirstChild().getNodeValue(); System.out.println("價格:" + price); } }
執行結果:code
價格:$50orm
八、根據序號查詢
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE book SYSTEM "test.dtd"> <!-- country 中國 count 印度 rights 版權全部 pricenotation $ type 支票或現金 默認爲現金 --> <books> <book> <name>xml 使用詳解</name> <author>成龍來自&country;</author> <publication>Mac &rights;</publication> <price type="支票">&pricenotation;50</price> </book> <book> <name>xml 揭祕</name> <author>Raghu 來自&count;</author> <publication>Mac &rights;</publication> <price>&pricenotation;45</price> </book> </books>
<!ELEMENT books (book+)> <!ELEMENT book (name,author,publication,price)> <!ELEMENT name (#PCDATA)> <!ELEMENT author (#PCDATA)> <!ELEMENT publication (#PCDATA)> <!ELEMENT price (#PCDATA)> <!ATTLIST price type (支票|現金) "現金" > <!ENTITY country "中國"> <!ENTITY count "印度"> <!ENTITY rights "版權全部"> <!ENTITY pricenotation "$">
package com.ljb.app.xml; import java.util.ArrayList; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; /** * 用DOM解析XML(查詢、增長、刪除、修改) * @author LJB * @version 2015年3月23日 */ public class DomParseXml { /** * @param args */ public static void main(String[] args) { getBookByNum(2); } /** * 獲取DOM樹 */ public static Document getDom () { try { // 建立解析器工廠實例 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 從DOM工廠得到解析器 DocumentBuilder db = dbf.newDocumentBuilder(); // 獲取DOM樹 Document doc = db.parse("src/main/java/third.xml"); return doc; } catch (Exception e) { e.printStackTrace(); return null; } } /** * 獲取list列表 * @return */ public static ArrayList<Book> getBookList () { NodeList bookNodeList = getDom().getElementsByTagName("book"); ArrayList<Book> bookList = new ArrayList<Book>(); for (int i = 0 ; i < bookNodeList.getLength() ; i++) { Book book = new Book(); Node bookNode = bookNodeList.item(i); Element bookElement = (Element)bookNode; String name = bookElement.getElementsByTagName("name").item(0).getFirstChild().getNodeValue(); String author = bookElement.getElementsByTagName("author").item(0).getFirstChild().getNodeValue(); String publication = bookElement.getElementsByTagName("publication").item(0).getFirstChild().getNodeValue(); String price = bookElement.getElementsByTagName("price").item(0).getFirstChild().getNodeValue(); book.setName(name); book.setAuthor(author); book.setPublication(publication); book.setPrice(price); bookList.add(book); } return bookList; } /** * 根據序號查詢書籍 * @param num */ public static void getBookByNum (int num) { ArrayList<Book> bookList = getBookList(); Book book = bookList.get(num-1); System.out.println(num + "\t" + book.getName() + "\t" + book.getAuthor() + "\t" + book.getPublication() + "\t" + book.getPrice()); } }
package com.ljb.app.xml; /** * 建立book對象 * @author LJB * @version 2015年3月23日 */ public class Book { private String name; private String author; private String publication; private String price; 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 getPublication() { return publication; } public void setPublication(String publication) { this.publication = publication; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } }
執行結果:
2 xml 揭祕 Raghu 來自印度 Mac 版權全部 $45
九、添加節點
package com.ljb.app.xml; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.ArrayList; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.CDATASection; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; /** * 用DOM解析XML(查詢、增長、刪除、修改) * @author LJB * @version 2015年3月23日 */ public class DomParseXml { /** * @param args */ public static void main(String[] args) { // find(); // getTextNode(); // getBookByNum(9); try { saveNode(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 獲取DOM樹 */ public static Document getDom () { try { // 建立解析器工廠實例 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 從DOM工廠得到解析器 DocumentBuilder db = dbf.newDocumentBuilder(); // 獲取DOM樹 Document doc = db.parse("src/main/java/third.xml"); return doc; } catch (Exception e) { e.printStackTrace(); return null; } } /** * 添加節點 * @throws FileNotFoundException * @throws TransformerException */ public static void saveNode () throws FileNotFoundException, TransformerException { Document doc = getDom(); // 建立book元素 Element bookElement = doc.createElement("book"); // 建立name元素 Element nameElement = doc.createElement("name"); Node nameNode = doc.createTextNode("java"); nameElement.appendChild(nameNode); // 建立author元素 Element authorElement = doc.createElement("author"); Node authorNode = doc.createTextNode("author來自中國"); authorElement.appendChild(authorNode); // 建立publication元素 Element publicationElement = doc.createElement("publication"); Node publicationNode = doc.createTextNode("java版權全部"); publicationElement.appendChild(publicationNode); // 建立price元素 Element priceElement = doc.createElement("price"); // CDATASection cdata = doc.createCDATASection("$90"); Node priceNode = doc.createTextNode("$90"); priceElement.appendChild(priceNode); // 添加父子關係 bookElement.appendChild(nameElement); bookElement.appendChild(authorElement); bookElement.appendChild(publicationElement); bookElement.appendChild(priceElement); Element root = (Element)doc.getElementsByTagName("books").item(0); root.appendChild(bookElement); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // 縮進 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); DOMSource domSource = new DOMSource(doc); StreamResult result = new StreamResult(new FileOutputStream("src/main/java/third.xml")); transformer.transform(domSource, result); } }
運行結果:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!-- country 中國 count 印度 rights 版權全部 pricenotation $ type 支票或現金 默認爲現金 --> <books> <book> <name>xml 使用詳解</name> <author>成龍來自中國</author> <publication>Mac 版權全部</publication> <price type="支票">$50</price> </book> <book> <name>xml 揭祕</name> <author>Raghu 來自印度</author> <publication>Mac 版權全部</publication> <price type="現金">$45</price> </book> <book> <name>java</name> <author>author來自中國</author> <publication>java版權全部</publication> <price>$90</price> </book> </books>
十、修改dom(根據序號修改書名)
package com.ljb.app.xml; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.ArrayList; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.CDATASection; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; /** * 用DOM解析XML(查詢、增長、刪除、修改) * @author LJB * @version 2015年3月23日 */ public class DomParseXml { /** * @param args */ public static void main(String[] args) { try { modify(2); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 獲取DOM樹 */ public static Document getDom () { try { // 建立解析器工廠實例 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 從DOM工廠得到解析器 DocumentBuilder db = dbf.newDocumentBuilder(); // 獲取DOM樹 Document doc = db.parse("src/main/java/third.xml"); return doc; } catch (Exception e) { e.printStackTrace(); return null; } } /** * 根據序號修改書名 * @throws FileNotFoundException * @throws TransformerException */ public static void modify (int num) throws FileNotFoundException, TransformerException { Document doc = getDom(); NodeList nameNodeList = doc.getElementsByTagName("name"); Node nameNode = nameNodeList.item(num-1); Element nameElement = (Element)nameNode; nameElement.getFirstChild().setNodeValue("修改後的書名"); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); DOMSource domSource = new DOMSource(doc); StreamResult result = new StreamResult(new FileOutputStream("src/main/java/third.xml")); transformer.transform(domSource, result); } }
執行結果;
<?xml version="1.0" encoding="UTF-8" standalone="no"?><!-- country 中國 count 印度 rights 版權全部 pricenotation $ type 支票或現金 默認爲現金 --><books> <book> <name>xml 使用詳解</name> <author>成龍來自中國</author> <publication>Mac 版權全部</publication> <price type="支票">$50</price> </book> <book> <name>修改後的書名</name> <author>Raghu 來自印度</author> <publication>Mac 版權全部</publication> <price type="現金">$45</price> </book> <book> <name>java</name> <author>author來自中國</author> <publication>java版權全部</publication> <price>$90</price> </book> </books>
十一、刪除節點(根據序號刪除書節點)
package com.ljb.app.xml; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.ArrayList; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.CDATASection; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; /** * 用DOM解析XML(查詢、增長、刪除、修改) * @author LJB * @version 2015年3月23日 */ public class DomParseXml { /** * @param args */ public static void main(String[] args) { try { delete(3); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 獲取DOM樹 */ public static Document getDom () { try { // 建立解析器工廠實例 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 從DOM工廠得到解析器 DocumentBuilder db = dbf.newDocumentBuilder(); // 獲取DOM樹 Document doc = db.parse("src/main/java/third.xml"); return doc; } catch (Exception e) { e.printStackTrace(); return null; } } /** * 刪除節點 * @param num * @throws FileNotFoundException * @throws TransformerException */ public static void delete (int num) throws FileNotFoundException, TransformerException { Document doc = getDom(); NodeList bookNodeList = doc.getElementsByTagName("book"); Node bookNode = bookNodeList.item(num-1); Element bookElement = (Element)bookNode; bookElement.getParentNode().removeChild(bookElement); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); DOMSource domSource = new DOMSource(doc); StreamResult result = new StreamResult(new FileOutputStream("src/main/java/third.xml")); transformer.transform(domSource, result); } }