Javaアクションxmlファイル

31472 ワード

Bbsxml.java

 
  1. publicclassBbsxml{
  2. privateStringimgsrc;
  3. privateStringtitle;
  4. privateStringurl;
  5. publicStringgetTitle(){
  6. returntitle;
  7. }
  8. publicvoidsetTitle(Stringtitle){
  9. this.title=title;
  10. }
  11. publicStringgetUrl(){
  12. returnurl;
  13. }
  14. publicvoidsetUrl(Stringurl){
  15. this.url=url;
  16. }
  17. publicStringgetImgsrc(){
  18. returnimgsrc;
  19. }
  20. publicvoidsetImgStringimgsrc){< /span>
  21. this.imgsrc=imgsrc;
  22. }
  23. publicstaticvoidmain(String[]args){
  24. //TODOAuto-generatedmethodstub
  25. }
  26. }

XmlOprate.java

 
  1. importjava.io.IOException;
  2. importjava.util.ArrayList;
  3. importjava.util.List;
  4. importjavax.xml.parsers.DocumentBuilder;
  5. importjavax.xml.parsers.DocumentBuilderFactory;
  6. importjavax.xml.parsers.ParserConfigurationException;
  7. importjavax.xml.transform.Transformer;
  8. importjavax.xml.transform.TransformerConfigurationException;
  9. importjavax.xml.transform.TransformerException;
  10. importjavax.xml.transform.TransformerFactory;
  11. importjavax.xml.transform.dom.DOMSource;
  12. importjavax.xml.transform.stream.StreamResult;
  13. importorg.w3c.dom.Document;
  14. importorg.w3c.dom.Element;
  15. importorg.w3c.dom.NodeList;
  16. importorg.w3c.dom.Text;
  17. importorg.xml.sax.SAXException;
  18. publicclassXmlOprate{
  19. Documentdoc;
  20. DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
  21. DocumentBuilderbuilder;
  22. NodeListimags;
  23. Stringpath;
  24. publicNodeListgetImags(){
  25. returnimags;
  26. }
  27. publicvoidsetImags(NodeListimags){
  28. this.imags=imags;
  29. }
  30. /**
  31. *
  32. *
  33. *@parampath
  34. *:xml
  35. *@paramnodes
  36. *: xml
  37. */
  38. publicXmlOprate(Stringpath){
  39. super();
  40. this.path=path;
  41. //System.out.println(System.getProperty("user.dir"));
  42. }
  43. /**
  44. * XML
  45. *
  46. *@parampath
  47. */
  48. publicvoidreadXml(){
  49. try{
  50. builder=factory.newDocumentBuilder();
  51. Documentdoc=builder.parse(path);
  52. doc.normalize();
  53. NodeListimags=doc.getElementsByTagName("imags");
  54. this.setImags(imags);
  55. for(inti=0;i<imags.getLength();i++){
  56. Elementlink=(Element)imags.item(i);
  57. System.out.print("imgsrc=\'#\'" /span>);
  58. System.out.println(link.getElementsByTagName("imgsrc").item(0)
  59. .getFirstChild().getNodeValue());
  60. System.out.print("title:");
  61. System.out.println(link.getElementsByTagName("title").item(0)
  62. .getFirstChild().getNodeValue());
  63. System.out.print("url:");
  64. System.out.println(link.getElementsByTagName("url").item(0)
  65. .getFirstChild().getNodeValue());
  66. System.out.println();
  67. }
  68. }catch(ParserConfigurationExceptione){
  69. e.printStackTrace();
  70. }catch(SAXExceptione){
  71. e.printStackTrace();
  72. }catch(IOExceptione){
  73. e.printStackTrace();
  74. }
  75. }
  76. /**
  77. *http://www.ablanxue.com XML, List ,Bbsxm
  78. * javabean, get,set l, ,
  79. *
  80. *@parampath
  81. */
  82. publicList<Bbsxml>xml(){
  83. List<Bbsxml>list=null;
  84. list=newArrayList<Bbsxml>();
  85. try{
  86. builder=factory.newDocumentBuilder();
  87. Documentdoc=builder.parse(path);
  88. doc.normalize();
  89. Bbsxmlbbsxml=null;
  90. NodeListimags=doc.getElementsByTagName("imags");
  91. this.setImags(imags);
  92. for(inti=imags.getLength()-1;i>-1;i--){
  93. Elementlink=(Element)imags.item(i);
  94. bbsxml=newBbsxml();
  95. bbsxml.setImglink.getElementsByTagName("imgsrc").item(0)
  96. .getFirstChild().getNodeValue());
  97. bbsxml.setTitle(link.getElementsByTagName("title").item(0)
  98. .getFirstChild().getNodeValue());
  99. bbsxml.setUrl(link.getElementsByTagName("url").item(0)
  100. .getFirstChild().getNodeValue());
  101. list.add(bbsxml);
  102. }
  103. }catch(ParserConfigurationExceptione){
  104. e.printStackTrace();
  105. }catch(SAXExceptione){
  106. e.printStackTrace();
  107. }catch(IOExceptione){
  108. e.printStackTrace();
  109. }
  110. returnlist;
  111. }
  112. /**
  113. *addCode
  114. *
  115. *@parampath
  116. */
  117. publicvoidaddXmlCode(Stringimgsrc,Stringtitle,Stringurl){
  118. try{
  119. builder=factory.newDocumentBuilder();
  120. Documentdoc=builder.parse(path);
  121. doc.normalize();
  122. Texttextseg;
  123. Elementimag=doc.createElement("imags");
  124. Elementlinkimgsrc=doc.createElement("imgsrc");
  125. textseg=doc.createTextNode(imgsrc);
  126. linkimgsrc.appendChild(textseg);
  127. imag.appendChild(linkimgsrc);
  128. Elementlinktitle=doc.createElement("title");
  129. textseg=doc.createTextNode(title);
  130. linktitle.appendChild(textseg);
  131. imag.appendChild(linktitle);
  132. Elementlinkurl=doc.createElement("url");
  133. textseg=doc.createTextNode(url);
  134. linkurl.appendChild(textseg);
  135. imag.appendChild(linkurl);
  136. doc.getDocumentElement().appendChild(imag);
  137. TransformerFactorytFactory=TransformerFactory.newInstance();
  138. Transformertransformer;
  139. transformer=tFactory.newTransformer();
  140. DOMSourcesource=newDOMSource(doc);
  141. StreamResultresult=newStreamResult(newjava.io.File(path));
  142. transformer.transform(source,result);
  143. }catch(Exceptione){
  144. }
  145. }
  146. /**
  147. *deletexmlcode
  148. *
  149. *@parampath
  150. */
  151. publicvoiddelXmlCode(){
  152. try{
  153. builder=factory.newDocumentBuilder();
  154. }catch(ParserConfigurationExceptione){
  155. //TODOAuto-generatedcatchblock
  156. e.printStackTrace();
  157. }
  158. try{
  159. doc=builder.parse(path);
  160. }catch(SAXExceptione){
  161. //TODOAuto-generatedcatchblock
  162. e.printStackTrace();
  163. }catch(IOExceptione){
  164. //TODOAuto-generatedcatchblock
  165. e.printStackTrace();
  166. }
  167. doc.normalize();
  168. NodeListimags=doc.getElementsByTagName("imags");
  169. //if(imags.getLength()>9){
  170. //
  171. Elementelink=(Element)imags.item(0);
  172. elink.removeChild(elink.getElementsByTagName("imgsrc").item(0));
  173. elink.removeChild(elink.getElementsByTagName("title").item(0));
  174. elink.removeChild(elink.getElementsByTagName("url").item(0));
  175. doc.getFirstChild().removeChild(elink);
  176. TransformerFactorytFactory=TransformerFactory.newInstance();
  177. Transformertransformer=null;
  178. try{
  179. transformer=tFactory.newTransformer();
  180. }catch(TransformerConfigurationExceptione){
  181. //TODOAuto-generatedcatchblock
  182. e.printStackTrace();
  183. }
  184. DOMSourcesource=newDOMSource(doc);
  185. StreamResultresult=newStreamResult(newjava.io.File(path));
  186. try{
  187. transformer.transform(source,result);
  188. }catch(TransformerExceptione){
  189. //TODOAuto-generatedcatchblock
  190. e.printStackTrace();
  191. }
  192. //}
  193. }
  194. publicstaticvoidmain(String[]args){
  195. XmlOpratexm=newXmlOprate("data.xml");
  196. //xm.readXml();
  197. //xm.addXmlCode("images/1.gif","xuexiii","http://www.baidu.com");
  198. xm.delXmlCode();
  199. }
  200. }

data.xml

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

JSPページ直接操作xml.jsp

 
  1. <%@pagelanguage="java"contentType="text/html;charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@pageimport="javax.xml.parsers.*"%>
  4. <%@pageimport="org.w3c.dom.*"%>
  5. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
  6. <html>
  7. <head>
  8. <metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
  9. <title>Inserttitlehere</title>
  10. <%
  11. //
  12. DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
  13. //
  14. DocumentBuilderbuilder=factory.newDocumentBuilder();
  15. // XML , Document
  16. Documentdoc=builder.parse(request.getRealPath("/")+"WEB-INF/data.xml");
  17. doc.normalize();
  18. // book
  19. NodeListbooks=doc.getElementsByTagName("book");
  20. %>
  21. </head>
  22. <body>
  23. <h2> </h2>
  24. <br>
  25. <%
  26. for(inti=0;i<books.getLength();i++){
  27. // book
  28. Elementbook=(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. Elementbookdate=(Element)book.getElementsByTagName("date").item(0);
  41. Stringday=book.getElementsByTagName("day").item(0).getFirstChild().getNodeValue();
  42. Stringmonth=book.getElementsByTagName("month").item(0).getFirstChild().getNodeValue();
  43. Stringyear=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>