SAX能否修改XML文件?
能否给个修改xml的例子(sax,或者dom)?

解决方案 »

  1.   

    http://hi.baidu.com/greenappleking/blog/item/e939d0087f7f2fa72fddd44d.html可以参考下
      

  2.   

    修改XML文件?我没有看错吧。它读取进去解析还能修改?
      

  3.   

    http://hi.baidu.com/greenappleking/blog/item/e939d0087f7f2fa72fddd44d.html
    顶上!
      

  4.   


    不知道楼主会不会解析xml在重新构造xml.public class XmlMaker {
    /**
     * method name:makeErrorResponse<BR>
     * method description: make a wrong response<BR>
     * Re:<BR>
     * 
     * @param <int> <the error code>
     * (<int>)
     * @return <String> <response message in xml format> 
     * (<String>)
     */
    public static String makeErrorResponse(int code) {
    StringBuffer sb = new StringBuffer();
    sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    sb.append("<data><response>" + code + "</response>");
    sb.append("</data>");
    return sb.toString();
    }
    /**
     * method name:makeUserXMLInfo<BR>
     * method description: make a response contains  user info<BR>
     * Re:<BR>
     * 
     * @param <User> <an instance of user>
     * (<User>)
     * @param <String> <the flag of user controling the msg>
     * (<String>)
     * @return <String> <response message in xml format> 
     * (<String>)
     */
    public static String makeUserXMLInfo(User user, String hasCfdInfo, int code) {
    StringBuffer sb = new StringBuffer();
    sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    sb.append("<data><response>" + code + "</response><user>"); sb.append("<id>" + user.getId() + "</id>" + "<username>"
    + user.getUsername() + "</username>" + "<mail>"
    + user.getMail() + "</mail>" + "<info>" + user.getInfo()
    + "</info>");
    if (hasCfdInfo.equals("true")) {
    sb.append("<password>" + user.getPassword() + "</password>"
    + "<birth>" + user.getBirthday() + "</birth>" + "<sex>"
    + user.getSex() + "</sex>");
    }
    sb.append("</user></data>");
    return sb.toString();
    }
    /**
     * method name:makeAppXMLInfo<BR>
     * method description: make a response contains  application info<BR>
     * Re:<BR>
     * 
     * @param <ApplicationItem> <the application turned to xml format>
     * (<ApplicationItem>)
     * @param <ServletContext> <the currrent context>
     * (<ServletContext>)
     * @param <int> <the response code to response to client>
     * (<int>)
     * @return <String> <response message in xml format> 
     * (<String>)
     */
    public static String makeAppXMLInfo(ApplicationItem item,
    ServletContext context, int code) {
    StringBuffer sb = new StringBuffer();
    sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    sb.append("<data><response>" + code + "</response><app>");
    sb.append("<name>" + (item.getName()) + "</name>"
    + "<author>" + (item.getAuthor())
    + "</author>" + "<price>" + item.getPrice() + "</price>"
    + "<rating>" + item.getRating() + "</rating>" + "<icon>"
    + BitMapTranslator.BIMToByte(item.getIcon(), context)
    + "</icon>" + "<info>" + item.getInfo() + "</info>"
    + "<preview>"
    + BitMapTranslator.PreToByte(item.getPreview(), context)
    + "</preview>" + "<id>" + item.getId() + "</id>" + "<version>"
    + item.getVersion() + "</version>" + "<pkgname>"
    + item.getPkgname() + "</pkgname>" + "<path>" + item.getPath()
    + "</path>"+"<downs>"+AppDao.getDownLoadNum(String.valueOf(item.getId()))+"</downs>");
    sb.append("</app></data>");
    return sb.toString();
    }
    /**
     * method name:makeAppXMLInfo<BR>
     * method description: make a response contains  applications info<BR>
     * Re:<BR>
     * 
     * @param <List> <the list of  applications>
     * (<ServletContext>)
     * @param <ServletContext> <the currrent context>
     * (<ServletContext>)
     * @param <int> <the response code to response to client>
     * (<int>)
     * @return <String> <response message in xml format> 
     * (<String>)
     */
    public static String makeAppXMLInfo(List<ApplicationItem> appList,
    ServletContext context,int code) {
    StringBuffer sb = new StringBuffer();
    sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    sb.append("<data><response>");
    sb.append(code);
    sb.append("</response><list>");
    for (ApplicationItem item : appList) {
    sb.append("<app><name>" + item.getName() + "</name>" + "<author>"
    +(item.getAuthor()) + "</author>"
    + "<price>" + item.getPrice() + "</price>" + "<rating>"
    + item.getRating() + "</rating>" +"<info>"+item.getInfo()+"</info>"+ "<icon>"
    + BitMapTranslator.BIMToByte(item.getIcon(), context)
    + "</icon>" + "<id>" + item.getId() + "</id></app>");
    }
    sb.append("</list></data>");
    return sb.toString();
    }
    /**
     * method name:makeEvaXMLInfo<BR>
     * method description: make a response contains  eval info<BR>
     * Re:<BR>
     * 
     * @param <List> <the list of evals>
     * (<List>)
     * @return <String> <response message in xml format> 
     * (<String>)
     */
    public static String makeEvaXMLInfo(List<Evaluation> evaList){
    StringBuffer sb = new StringBuffer();
    sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    sb.append("<data><response>1009</response><list>");
    for(Evaluation eva:evaList){
    sb.append("<evaluation><id>"+eva.getAppID()+"</id>"+"<name>"+eva.getEnName()+"</name>"
      +"<eva>"+(eva.getEva())+"</eva>"+"<date>"+eva.getDate()+"</date>"+"<rating>"+eva.getRating()+"</rating>"
    +"</evaluation>");
    }
    sb.append("</list></data>");
    return sb.toString();
    }
    }
    解析:
    [code=Java]
    public class XmlParser { /**
     * method name:parserAppListXml<BR>
     * method description:parse xml of applications<BR>
     * Re:<BR>
     * 
     * @param <InputSource> <the inputsource of xml>
     * (<InputSource>)
     * @return <AppListHandler> <the result of parsing>
     * (<AppListHandler>)
     * @exception <ParserConfigurationException> <if no matching SAXParser could be found>
     * (<ParserConfigurationException>)
     * @exception <SAXException> <if creating the SAXParser failed due to some other reason>
     * (<SAXException>)
     * @exception <IOException> <exception of IO>
     * (<IOException>)
     */
    public AppListHandler parserAppListXml(InputSource is)
    throws ParserConfigurationException, SAXException, IOException { AppListHandler appListHandler = new AppListHandler(); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser; parser = factory.newSAXParser(); XMLReader xmlreader = parser.getXMLReader(); xmlreader.setContentHandler(appListHandler); xmlreader.parse(is); return appListHandler;
    } /**
     * method name:parserUserInfoXml<BR>
     * method description:parse xml of user<BR>
     * Re:<BR>
     * 
     * @param <InputSource> <the inputsource of xml>
     * (<InputSource>)
     * @return <UserHandler> <the result of parsing>
     * (<UserHandler>)
     * @exception <ParserConfigurationException> <if no matching SAXParser could be found>
     * (<ParserConfigurationException>)
     * @exception <SAXException> <if creating the SAXParser failed due to some other reason>
     * (<SAXException>)
     * @exception <IOException> <exception of IO>
     * (<IOException>)
     */
    public UserHandler parserUserInfoXml(InputSource is)
    throws ParserConfigurationException, SAXException, IOException { UserHandler userHandler = new UserHandler(); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser; parser = factory.newSAXParser(); XMLReader xmlreader = parser.getXMLReader(); xmlreader.setContentHandler(userHandler); xmlreader.parse(is); return userHandler;
    } /**
     * method name:parserAppInfoXml<BR>
     * method description:parse xml of application<BR>
     * Re:<BR>
     * 
     * @param <InputSource> <the inputsource of xml>
     * (<InputSource>)
     * @return <AppHandler> <the result of parsing>
     * (<AppHandler>)
     * @exception <ParserConfigurationException> <if no matching SAXParser could be found>
     * (<ParserConfigurationException>)
     * @exception <SAXException> <if creating the SAXParser failed due to some other reason>
     * (<SAXException>)
     * @exception <IOException> <exception of IO>
     * (<IOException>)
     */
    public AppHandler parserAppInfoXml(InputSource is)
    throws ParserConfigurationException, SAXException, IOException { AppHandler appHandler = new AppHandler(); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser; parser = factory.newSAXParser(); XMLReader xmlreader = parser.getXMLReader(); xmlreader.setContentHandler(appHandler); xmlreader.parse(is); return appHandler;
    } /**
     * method name:parserEvaListXml<BR>
     * method description:parse xml of evaluation<BR>
     * Re:<BR>
     * 
     * @param <InputSource> <the inputsource of xml>
     * (<InputSource>)
     * @return <EvaListHandler> <the result of parsing>
     * (<EvaListHandler>)
     * @exception <ParserConfigurationException> <if no matching SAXParser could be found>
     * (<ParserConfigurationException>)
     * @exception <SAXException> <if creating the SAXParser failed due to some other reason>
     * (<SAXException>)
     * @exception <IOException> <exception of IO>
     * (<IOException>)
     */
    public EvaListHandler parserEvaListXml(InputSource is)
    throws ParserConfigurationException, SAXException, IOException { EvaListHandler evaListHandler = new EvaListHandler(); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser; parser = factory.newSAXParser(); XMLReader xmlreader = parser.getXMLReader(); xmlreader.setContentHandler(evaListHandler); xmlreader.parse(is); return evaListHandler;
    }}
    [/code]
      

  5.   

    sax技术不能修改xml,只是读取xml文件比dom的方式更加节省内存。你也可以使用pull的方式操作xml文件。jdk1.6的新特性。