XML reading and DWR implementation

Source: Internet
Author: User
  1. Web. xml file:
    <?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><servlet><servlet-name>dwr</servlet-name><servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class><init-param><param-name>debug</param-name><param-value>true</param-value></init-param></servlet><servlet-mapping><servlet-name>dwr</servlet-name><url-pattern>/dwr/*</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>

  2. DWR. xml file
    <?xml version="1.0" encoding="UTF-8"?><dwr><allow>   <create creator="new" javascript="bankService">   <param name="class" value="com.xihangzh.xml.XmlService"/>   </create>   <convert converter="bean" match="com.xihangzh.xml.Bank"/></allow></dwr>

  3. Bank.html File
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. Xmlservice class
    package com.xihangzh.xml;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.OutputStreamWriter;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.SAXReader;import org.dom4j.io.XMLWriter;public class XmlService {private static Document document;static{System.out.println("this is static!!!");SAXReader reader = new SAXReader();File file = new File("d:/test.xml");try {document = reader.read(file);} catch (DocumentException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public Bank getBankById(int id) {Element root = document.getRootElement();Bank bank = null;for(Iterator<Element> iter = root.elementIterator(); iter.hasNext(); ) {Element child = iter.next();if(String.valueOf(id).equals(child.element("id").getText())) {bank = new Bank(); bank.setId(Integer.parseInt(child.element("id").getText())); bank.setIde(child.element("ide").getText()); bank.setName(child.element("name").getText()); bank.setAddress(child.element("address").getText()); bank.setScale(child.element("scale").getText());}}return bank;}public String delBank(int id) {Element root = document.getRootElement();String flag = "didn't deleted";for(Iterator<Element> iter = root.elementIterator(); iter.hasNext(); ) { Element child = iter.next(); if(String.valueOf(id).equals(child.element("id").getText())) { root.remove(child); this.persistentBank(document); flag = "deleted"; }  }return flag;}public List getList(){ Element root = document.getRootElement(); List<Bank> bankList = new ArrayList<Bank>(); for(Iterator<Element> iter = root.elementIterator(); iter.hasNext(); ) { Element child = iter.next(); Bank bank = new Bank(); bank.setId(Integer.parseInt(child.element("id").getText())); bank.setIde(child.element("ide").getText()); bank.setName(child.element("name").getText()); bank.setAddress(child.element("address").getText()); bank.setScale(child.element("scale").getText()); bankList.add(bank); } return bankList;}public String add(Bank bank) {Element root = document.getRootElement();Element newBank = root.addElement("bank");newBank.addElement("id").setText(String.valueOf(bank.getId()));newBank.addElement("ide").setText(bank.getIde());newBank.addElement("name").setText(bank.getName());newBank.addElement("address").setText(bank.getAddress());newBank.addElement("scale").setText(bank.getScale());persistentBank(document);return "add success";}public String modify(Bank bank) {Element root = document.getRootElement();String flag = "modify failed";for(Iterator<Element> iter = root.elementIterator(); iter.hasNext(); ) {Element child = iter.next();if(String.valueOf(bank.getId()).equals(child.element("id").getText())) {child.element("ide").setText(bank.getIde());child.element("name").setText(bank.getName());child.element("address").setText(bank.getAddress());child.element("scale").setText(bank.getScale());persistentBank(document);flag = "modify successed";}}return flag;}private void persistentBank(Document document){try {XMLWriter write = new XMLWriter(new OutputStreamWriter(new FileOutputStream("d:/test.xml"), "UTF-8"));write.write(document);write.flush();write.close();} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (FileNotFoundException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}}}

  5. XML file to save
    <? XML version = "1.0" encoding = "UTF-8"?> <Banks banksatt = "A"> <bank bankatt = "hello"> <ID Haha = "Haha"> 1 </ID> <ide> A </ide> <name = "Hua"> Agricultural Bank </Name> <address> Haidian District </address> <scale> large </scale> </Bank> <bank> <ID> 4 </ id> <ide> B </ide> <Name> China Merchants Bank </Name> <address> Changping District </address> <scale scaleatt = "5"> middle </scale> </Bank> <bank> <ID> 5 </ID> <ide> C </ide> <Name> Industrial and Commercial Bank </Name> <address> Chaoyang District </address> <Scale> little </scale> </Bank> </banks>

    The entire project structure is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.