SAX能否修改XML文件,能否给个修改xml的例子(sax,或Dom)? SAX能否修改XML文件?能否给个修改xml的例子(sax,或者dom)? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://hi.baidu.com/greenappleking/blog/item/e939d0087f7f2fa72fddd44d.html可以参考下 修改XML文件?我没有看错吧。它读取进去解析还能修改? http://hi.baidu.com/greenappleking/blog/item/e939d0087f7f2fa72fddd44d.html顶上! 不知道楼主会不会解析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] sax技术不能修改xml,只是读取xml文件比dom的方式更加节省内存。你也可以使用pull的方式操作xml文件。jdk1.6的新特性。 Android中如何保存并恢复WebView的内容? 求助:java.lang.NoClassdefFoundError:org.apache.cxf.jaxws.JaxWsProxyFactoryBean错误 谷歌商店如何收费? android 调用系统相机问题 android 不能调用cxf服务 怎样让一个账号不能同时登陆呢 android源码及emulator源码编译问题 急!!求各位解释下 非常感谢 Android短彩信数据库mmssms.db数据库中的raw表存放的是什么数据,用作何用? android短信收件人UI效果 汉字识别 问题 Android关于点击图片出现按钮的问题
顶上!
不知道楼主会不会解析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]