Javaアクションxmlファイル
31472 ワード
Bbsxml.java
XmlOprate.java
data.xml
JSPページ直接操作xml.jsp
- publicclassBbsxml{
- privateStringimgsrc;
- privateStringtitle;
- privateStringurl;
- publicStringgetTitle(){
- returntitle;
- }
- publicvoidsetTitle(Stringtitle){
- this.title=title;
- }
- publicStringgetUrl(){
- returnurl;
- }
- publicvoidsetUrl(Stringurl){
- this.url=url;
- }
- publicStringgetImgsrc(){
- returnimgsrc;
- }
- publicvoidsetImgStringimgsrc){< /span>
- this.imgsrc=imgsrc;
- }
- publicstaticvoidmain(String[]args){
- //TODOAuto-generatedmethodstub
- }
- }
XmlOprate.java
- importjava.io.IOException;
- importjava.util.ArrayList;
- importjava.util.List;
- importjavax.xml.parsers.DocumentBuilder;
- importjavax.xml.parsers.DocumentBuilderFactory;
- importjavax.xml.parsers.ParserConfigurationException;
- importjavax.xml.transform.Transformer;
- importjavax.xml.transform.TransformerConfigurationException;
- importjavax.xml.transform.TransformerException;
- importjavax.xml.transform.TransformerFactory;
- importjavax.xml.transform.dom.DOMSource;
- importjavax.xml.transform.stream.StreamResult;
- importorg.w3c.dom.Document;
- importorg.w3c.dom.Element;
- importorg.w3c.dom.NodeList;
- importorg.w3c.dom.Text;
- importorg.xml.sax.SAXException;
- publicclassXmlOprate{
- Documentdoc;
- DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
- DocumentBuilderbuilder;
- NodeListimags;
- Stringpath;
- publicNodeListgetImags(){
- returnimags;
- }
- publicvoidsetImags(NodeListimags){
- this.imags=imags;
- }
- /**
- *
- *
- *@parampath
- *:xml
- *@paramnodes
- *: xml
- */
- publicXmlOprate(Stringpath){
- super();
- this.path=path;
- //System.out.println(System.getProperty("user.dir"));
- }
- /**
- * XML
- *
- *@parampath
- */
- publicvoidreadXml(){
- try{
- builder=factory.newDocumentBuilder();
- Documentdoc=builder.parse(path);
- doc.normalize();
- NodeListimags=doc.getElementsByTagName("imags");
- this.setImags(imags);
- for(inti=0;i<imags.getLength();i++){
- Elementlink=(Element)imags.item(i);
- System.out.print("imgsrc=\'#\'" /span>);
- System.out.println(link.getElementsByTagName("imgsrc").item(0)
- .getFirstChild().getNodeValue());
- System.out.print("title:");
- System.out.println(link.getElementsByTagName("title").item(0)
- .getFirstChild().getNodeValue());
- System.out.print("url:");
- System.out.println(link.getElementsByTagName("url").item(0)
- .getFirstChild().getNodeValue());
- System.out.println();
- }
- }catch(ParserConfigurationExceptione){
- e.printStackTrace();
- }catch(SAXExceptione){
- e.printStackTrace();
- }catch(IOExceptione){
- e.printStackTrace();
- }
- }
- /**
- *http://www.ablanxue.com XML, List ,Bbsxm
- * javabean, get,set l, ,
- *
- *@parampath
- */
- publicList<Bbsxml>xml(){
- List<Bbsxml>list=null;
- list=newArrayList<Bbsxml>();
- try{
- builder=factory.newDocumentBuilder();
- Documentdoc=builder.parse(path);
- doc.normalize();
- Bbsxmlbbsxml=null;
- NodeListimags=doc.getElementsByTagName("imags");
- this.setImags(imags);
- for(inti=imags.getLength()-1;i>-1;i--){
- Elementlink=(Element)imags.item(i);
- bbsxml=newBbsxml();
- bbsxml.setImglink.getElementsByTagName("imgsrc").item(0)
- .getFirstChild().getNodeValue());
- bbsxml.setTitle(link.getElementsByTagName("title").item(0)
- .getFirstChild().getNodeValue());
- bbsxml.setUrl(link.getElementsByTagName("url").item(0)
- .getFirstChild().getNodeValue());
- list.add(bbsxml);
- }
- }catch(ParserConfigurationExceptione){
- e.printStackTrace();
- }catch(SAXExceptione){
- e.printStackTrace();
- }catch(IOExceptione){
- e.printStackTrace();
- }
- returnlist;
- }
- /**
- *addCode
- *
- *@parampath
- */
- publicvoidaddXmlCode(Stringimgsrc,Stringtitle,Stringurl){
- try{
- builder=factory.newDocumentBuilder();
- Documentdoc=builder.parse(path);
- doc.normalize();
- Texttextseg;
- Elementimag=doc.createElement("imags");
- Elementlinkimgsrc=doc.createElement("imgsrc");
- textseg=doc.createTextNode(imgsrc);
- linkimgsrc.appendChild(textseg);
- imag.appendChild(linkimgsrc);
- Elementlinktitle=doc.createElement("title");
- textseg=doc.createTextNode(title);
- linktitle.appendChild(textseg);
- imag.appendChild(linktitle);
- Elementlinkurl=doc.createElement("url");
- textseg=doc.createTextNode(url);
- linkurl.appendChild(textseg);
- imag.appendChild(linkurl);
- doc.getDocumentElement().appendChild(imag);
- TransformerFactorytFactory=TransformerFactory.newInstance();
- Transformertransformer;
- transformer=tFactory.newTransformer();
- DOMSourcesource=newDOMSource(doc);
- StreamResultresult=newStreamResult(newjava.io.File(path));
- transformer.transform(source,result);
- }catch(Exceptione){
- }
- }
- /**
- *deletexmlcode
- *
- *@parampath
- */
- publicvoiddelXmlCode(){
- try{
- builder=factory.newDocumentBuilder();
- }catch(ParserConfigurationExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- try{
- doc=builder.parse(path);
- }catch(SAXExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }catch(IOExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- doc.normalize();
- NodeListimags=doc.getElementsByTagName("imags");
- //if(imags.getLength()>9){
- //
- Elementelink=(Element)imags.item(0);
- elink.removeChild(elink.getElementsByTagName("imgsrc").item(0));
- elink.removeChild(elink.getElementsByTagName("title").item(0));
- elink.removeChild(elink.getElementsByTagName("url").item(0));
- doc.getFirstChild().removeChild(elink);
- TransformerFactorytFactory=TransformerFactory.newInstance();
- Transformertransformer=null;
- try{
- transformer=tFactory.newTransformer();
- }catch(TransformerConfigurationExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- DOMSourcesource=newDOMSource(doc);
- StreamResultresult=newStreamResult(newjava.io.File(path));
- try{
- transformer.transform(source,result);
- }catch(TransformerExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- //}
- }
- publicstaticvoidmain(String[]args){
- XmlOpratexm=newXmlOprate("data.xml");
- //xm.readXml();
- //xm.addXmlCode("images/1.gif","xuexiii","http://www.baidu.com");
- xm.delXmlCode();
- }
- }
data.xml
- <?xmlversion="1.0"encoding="UTF-8"standalone="no"?>
- <root>
- <imags>
- <imgsrc>images/ad-02.jpg</imgsrc>
- <title>java xml </title>
- <url>http://www.ablanxue.com</url>
- </imags>
- <imags>
- <imgsrc>images/1.gif</imgsrc>
- <title>xuexiii</title>
- <url>http://www.baidu.com</url>
- </imags>
- </root>
JSPページ直接操作xml.jsp
- <%@pagelanguage="java"contentType="text/html;charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@pageimport="javax.xml.parsers.*"%>
- <%@pageimport="org.w3c.dom.*"%>
- <!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
- <title>Inserttitlehere</title>
- <%
- //
- DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
- //
- DocumentBuilderbuilder=factory.newDocumentBuilder();
- // XML , Document
- Documentdoc=builder.parse(request.getRealPath("/")+"WEB-INF/data.xml");
- doc.normalize();
- // book
- NodeListbooks=doc.getElementsByTagName("book");
- %>
- </head>
- <body>
- <h2> </h2>
- <br>
- <%
- for(inti=0;i<books.getLength();i++){
- // book
- Elementbook=(Element)books.item(i);
- // book ,
- out.print("title:");
- out.println(book.getElementsByTagName("title").item(0).getFirstChild().getNodeValue());
- out.print("<br>");
- out.print("URL:");
- out.println(book.getElementsByTagName("url").item(0).getFirstChild().getNodeValue());
- out.print("<br>");
- out.print("Author:");
- out.println(book.getElementsByTagName("author").item(0).getFirstChild().getNodeValue());
- out.print("<br>");
- out.print("Date:");
- Elementbookdate=(Element)book.getElementsByTagName("date").item(0);
- Stringday=book.getElementsByTagName("day").item(0).getFirstChild().getNodeValue();
- Stringmonth=book.getElementsByTagName("month").item(0).getFirstChild().getNodeValue();
- Stringyear=book.getElementsByTagName("year").item(0).getFirstChild().getNodeValue();
- out.println(day+"-"+month+"-"+year);
- out.print("<br>");
- out.print("Description:");
- out.println(book.getElementsByTagName("description").item(0).getFirstChild().getNodeValue());
- out.print("<br><br>");
- }
- %>
- </body>
- </html>