Bbsxml.java

  1. public class Bbsxml { 
  2. private String imgsrc; 
  3. private String title; 
  4. private String url; 
  5.  
  6.     public String getTitle() { 
  7.     return title; 
  8. public void setTitle(String title) { 
  9.     this.title = title; 
  10. public String getUrl() { 
  11.     return url; 
  12. public void setUrl(String url) { 
  13.     this.url = url; 
  14.     public String getImgsrc() { 
  15.     return imgsrc; 
  16. public void setImgString imgsrc) { < /span>
  17.     this.imgsrc = imgsrc; 
  18.  
  19.     public static void main(String[] args) { 
  20.         // TODO Auto-generated method stub 
  21.  
  22.     } 
  23.  

XmlOprate.java

  1. import java.io.IOException; 
  2. import java.util.ArrayList; 
  3. import java.util.List; 
  4.  
  5. import javax.xml.parsers.DocumentBuilder; 
  6. import javax.xml.parsers.DocumentBuilderFactory; 
  7. import javax.xml.parsers.ParserConfigurationException; 
  8. import javax.xml.transform.Transformer; 
  9. import javax.xml.transform.TransformerConfigurationException; 
  10. import javax.xml.transform.TransformerException; 
  11. import javax.xml.transform.TransformerFactory; 
  12. import javax.xml.transform.dom.DOMSource; 
  13. import javax.xml.transform.stream.StreamResult; 
  14. import org.w3c.dom.Document; 
  15. import org.w3c.dom.Element; 
  16. import org.w3c.dom.NodeList; 
  17. import org.w3c.dom.Text; 
  18. import org.xml.sax.SAXException; 
  19.  
  20. public class XmlOprate { 
  21.     Document doc; 
  22.     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
  23.     DocumentBuilder builder; 
  24.     NodeList imags; 
  25.     String path; 
  26.  
  27.     public NodeList getImags() { 
  28.         return imags; 
  29.     } 
  30.  
  31.     public void setImags(NodeList imags) { 
  32.         this.imags = imags; 
  33.     } 
  34.  
  35.     /** 
  36.      * 构造方法 
  37.      *  
  38.      * @param path 
  39.      *            :xml文件的路径 
  40.      * @param nodes 
  41.      *            :要解析的xml节点名称 
  42.      */ 
  43.     public XmlOprate(String path) { 
  44.         super(); 
  45.         this.path = path; 
  46.         // System.out.println(System.getProperty("user.dir")); 
  47.     } 
  48.  
  49.     /** 
  50.      * 解析XML 
  51.      *  
  52.      * @param path 
  53.      */ 
  54.     public void readXml() { 
  55.         try { 
  56.             builder = factory.newDocumentBuilder(); 
  57.             Document doc = builder.parse(path); 
  58.             doc.normalize(); 
  59.  
  60.             NodeList imags = doc.getElementsByTagName("imags"); 
  61.             this.setImags(imags); 
  62.             for (int i = 0; i < imags.getLength(); i++) { 
  63.                 Element link = (Element) imags.item(i); 
  64.                 System.out.print("imgsrc: "); 
  65.                 System.out.println(link.getElementsByTagName("imgsrc").item(0
  66.                         .getFirstChild().getNodeValue()); 
  67.                 System.out.print("title: "); 
  68.                 System.out.println(link.getElementsByTagName("title").item(0
  69.                         .getFirstChild().getNodeValue()); 
  70.                 System.out.print("url: "); 
  71.                 System.out.println(link.getElementsByTagName("url").item(0
  72.                         .getFirstChild().getNodeValue()); 
  73.                 System.out.println(); 
  74.             } 
  75.         } catch (ParserConfigurationException e) { 
  76.             e.printStackTrace(); 
  77.         } catch (SAXException e) { 
  78.             e.printStackTrace(); 
  79.         } catch (IOException e) { 
  80.             e.printStackTrace(); 
  81.         } 
  82.  
  83.     } 
  84.  
  85.     /** 
  86.      * http://www.ablanxue.com 解析XML,返回List集合,Bbsxm 
  87.      * 只是一个普通的javabean,只提供get,set的方法l,在此不写出,可自己创建了 
  88.      *  
  89.      * @param path 
  90.      */ 
  91.  
  92.     public List<Bbsxml> xml() { 
  93.         List<Bbsxml> list = null
  94.         list = new ArrayList<Bbsxml>(); 
  95.         try { 
  96.             builder = factory.newDocumentBuilder(); 
  97.             Document doc = builder.parse(path); 
  98.             doc.normalize(); 
  99.             Bbsxml bbsxml = null
  100.             NodeList imags = doc.getElementsByTagName("imags"); 
  101.             this.setImags(imags); 
  102.             for (int i = imags.getLength() - 1; i > -1; i--) { 
  103.                 Element link = (Element) imags.item(i); 
  104.  
  105.                 bbsxml = new Bbsxml(); 
  106.                 bbsxml.setImglink.getElementsByTagName("imgsrc").item(0
  107.                         .getFirstChild().getNodeValue()); 
  108.                 bbsxml.setTitle(link.getElementsByTagName("title").item(0
  109.                         .getFirstChild().getNodeValue()); 
  110.                 bbsxml.setUrl(link.getElementsByTagName("url").item(0
  111.                         .getFirstChild().getNodeValue()); 
  112.                 list.add(bbsxml); 
  113.  
  114.             } 
  115.         } catch (ParserConfigurationException e) { 
  116.             e.printStackTrace(); 
  117.         } catch (SAXException e) { 
  118.             e.printStackTrace(); 
  119.         } catch (IOException e) { 
  120.             e.printStackTrace(); 
  121.         } 
  122.  
  123.         return list; 
  124.     } 
  125.  
  126.     /** 
  127.      * addCode 
  128.      *  
  129.      * @param path 
  130.      */ 
  131.     public void addXmlCode(String imgsrc, String title, String url) { 
  132.  
  133.         try { 
  134.             builder = factory.newDocumentBuilder(); 
  135.             Document doc = builder.parse(path); 
  136.             doc.normalize(); 
  137.  
  138.             Text textseg; 
  139.             Element imag = doc.createElement("imags"); 
  140.  
  141.             Element linkimgsrc = doc.createElement("imgsrc"); 
  142.             textseg = doc.createTextNode(imgsrc); 
  143.             linkimgsrc.appendChild(textseg); 
  144.             imag.appendChild(linkimgsrc); 
  145.  
  146.             Element linktitle = doc.createElement("title"); 
  147.             textseg = doc.createTextNode(title); 
  148.             linktitle.appendChild(textseg); 
  149.             imag.appendChild(linktitle); 
  150.  
  151.             Element linkurl = doc.createElement("url"); 
  152.             textseg = doc.createTextNode(url); 
  153.             linkurl.appendChild(textseg); 
  154.             imag.appendChild(linkurl); 
  155.  
  156.             doc.getDocumentElement().appendChild(imag); 
  157.  
  158.             TransformerFactory tFactory = TransformerFactory.newInstance(); 
  159.             Transformer transformer; 
  160.  
  161.             transformer = tFactory.newTransformer(); 
  162.             DOMSource source = new DOMSource(doc); 
  163.             StreamResult result = new StreamResult(new java.io.File(path)); 
  164.             transformer.transform(source, result); 
  165.         } catch (Exception e) { 
  166.  
  167.         } 
  168.     } 
  169.  
  170.     /** 
  171.      * delete xml code 
  172.      *  
  173.      * @param path 
  174.      */ 
  175.     public void delXmlCode() { 
  176.  
  177.         try { 
  178.             builder = factory.newDocumentBuilder(); 
  179.         } catch (ParserConfigurationException e) { 
  180.             // TODO Auto-generated catch block 
  181.             e.printStackTrace(); 
  182.         } 
  183.         try { 
  184.             doc = builder.parse(path); 
  185.         } catch (SAXException e) { 
  186.             // TODO Auto-generated catch block 
  187.             e.printStackTrace(); 
  188.         } catch (IOException e) { 
  189.             // TODO Auto-generated catch block 
  190.             e.printStackTrace(); 
  191.         } 
  192.         doc.normalize(); 
  193.         NodeList imags = doc.getElementsByTagName("imags"); 
  194.  
  195.         // if (imags.getLength() > 9) { 
  196.         // 删除节点 
  197.         Element elink = (Element) imags.item(0); 
  198.         elink.removeChild(elink.getElementsByTagName("imgsrc").item(0)); 
  199.         elink.removeChild(elink.getElementsByTagName("title").item(0)); 
  200.         elink.removeChild(elink.getElementsByTagName("url").item(0)); 
  201.  
  202.         doc.getFirstChild().removeChild(elink); 
  203.  
  204.         TransformerFactory tFactory = TransformerFactory.newInstance(); 
  205.         Transformer transformer = null
  206.         try { 
  207.             transformer = tFactory.newTransformer(); 
  208.         } catch (TransformerConfigurationException e) { 
  209.             // TODO Auto-generated catch block 
  210.             e.printStackTrace(); 
  211.         } 
  212.         DOMSource source = new DOMSource(doc); 
  213.         StreamResult result = new StreamResult(new java.io.File(path)); 
  214.         try { 
  215.             transformer.transform(source, result); 
  216.         } catch (TransformerException e) { 
  217.             // TODO Auto-generated catch block 
  218.             e.printStackTrace(); 
  219.         } 
  220.         // } 
  221.  
  222.     } 
  223.  
  224.     public static void main(String[] args) { 
  225.         XmlOprate xm = new XmlOprate("data.xml"); 
  226.         // xm.readXml(); 
  227.         // xm.addXmlCode("p_w_picpaths/1.gif", "xuexiii", "http://www.baidu.com"); 
  228.         xm.delXmlCode(); 
  229.     } 
  230.  

data.xml

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?> 
  2. <root> 
  3.     <imags> 
  4.         <imgsrc>p_w_picpaths/ad-02.jpg</imgsrc> 
  5.         <title>java读取xml文件</title> 
  6.         <url>http://www.ablanxue.com</url> 
  7.     </imags> 
  8.     <imags> 
  9.         <imgsrc>p_w_picpaths/1.gif</imgsrc> 
  10.         <title>xuexiii</title> 
  11.         <url>http://www.baidu.com</url> 
  12.         </imags> 
  13.     </root> 

 

 

JSP页面直接操作 xml.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" 
  2.     pageEncoding="UTF-8"%> 
  3. <%@ page import="javax.xml.parsers.*"%> 
  4. <%@ page import="org.w3c.dom.*"%> 
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
  6. <html> 
  7. <head> 
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  9. <title>Insert title here</title> 
  10. <
  11.     //建立一个解析器工厂 
  12.     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
  13.     //获得一个具体的解析器对象 
  14.     DocumentBuilder builder = factory.newDocumentBuilder(); 
  15.     //对XML文档进行解析,获得Document对象 
  16.     Document doc = builder.parse(request.getRealPath("/")+ "WEB-INF/data.xml"); 
  17.     doc.normalize(); 
  18.     //获取所有的book元素列表 
  19.     NodeList books = doc.getElementsByTagName("book"); 
  20. %> 
  21. </head> 
  22. <body> 
  23.     <h2>图书列表</h2> 
  24.     <br> 
  25.     <
  26.         for (int i = 0; i < books.getLength(); i++) { 
  27.             //获取一个book元素 
  28.             Element book = (Element) books.item(i); 
  29.             //以下获取book的子元素,并输出 
  30.             out.print("title:"); 
  31.             out.println(book.getElementsByTagName("title").item(0).getFirstChild().getNodeValue()); 
  32.             out.print("<br>"); 
  33.             out.print("URL:"); 
  34.             out.println(book.getElementsByTagName("url").item(0).getFirstChild().getNodeValue()); 
  35.             out.print("<br>"); 
  36.             out.print("Author:"); 
  37.             out.println(book.getElementsByTagName("author").item(0).getFirstChild().getNodeValue()); 
  38.             out.print("<br>"); 
  39.             out.print("Date:"); 
  40.             Element bookdate = (Element) book.getElementsByTagName("date").item(0); 
  41.             String day = book.getElementsByTagName("day").item(0).getFirstChild().getNodeValue(); 
  42.             String month = book.getElementsByTagName("month").item(0).getFirstChild().getNodeValue(); 
  43.             String year = book.getElementsByTagName("year").item(0).getFirstChild().getNodeValue(); 
  44.             out.println(day + "-" + month + "-" + year); 
  45.             out.print("<br>"); 
  46.             out.print("Description:"); 
  47.             out.println(book.getElementsByTagName("description").item(0).getFirstChild().getNodeValue()); 
  48.             out.print("<br><br>"); 
  49.         } 
  50.     %> 
  51. </body> 
  52. </html>