Java修改xml后保存
2012-06-20 11:55
225 查看
直接上代码,不费话了:
希望对各位有帮助!
public static void main(String[] args) { Document doc = null; // TODO 自动生成方法存根 String xstr = "D:\\eclipse_workspace\\ImageServer\\src\\com\\aliyun\\security\\service\\image.xml"; // 使应用程序能够从 XML 文档获取生成 DOM 对象树的解析器 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { // 使其从 XML 文档获取 DOM 文档实例 DocumentBuilder db = dbf.newDocumentBuilder(); File f = new File(xstr); doc = db.parse(f); } catch (ParserConfigurationException e) { // TODO 自动生成 catch 块 e.printStackTrace(); System.out.println("无法创建DocumentBuilder!!"); } catch (SAXException e) { // TODO 自动生成 catch 块 e.printStackTrace(); System.out.println("该文件不是XML文件!!"); } catch (IOException e) { // TODO 自动生成 catch 块 System.out.println("文件读写时发生了错误!!"); e.printStackTrace(); } // 格式化文档 doc.getDocumentElement().normalize(); System.out.println("XML 文件结构正确!"); // 得到根结点 Node root = doc.getFirstChild(); // Element root = doc.getDocumentElement(); System.out.println("根结点是:" + root.getNodeName()); if (root.getNodeType() == Node.ELEMENT_NODE) { NodeList nodelist = root.getChildNodes(); for (int i = 0; i < nodelist.getLength(); i++) { Node node = nodelist.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { if (node.getNodeName().equals("bean")) { if (node.getAttributes().getNamedItem("id") .getNodeValue().equals("largeParameter")) { NodeList nodelist1 = node.getChildNodes(); for (int j = 0; j < nodelist1.getLength(); j++) { Node node1 = nodelist1.item(j); if (node1.getNodeType() == Node.ELEMENT_NODE) { if (node1.getNodeName().equals("property")) { if (node1.getAttributes() .getNamedItem("name") .getNodeValue().equals("width")) { node1.getAttributes() .getNamedItem("value") .setNodeValue("00000"); } } } } } } } } } try { TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer transformer = tfactory.newTransformer(); DOMSource source = new DOMSource(root); /* 新文件的地址 */ File file = new File( "D:\\eclipse_workspace\\ImageServer\\src\\com\\aliyun\\security\\service\\image.xml"); StreamResult result = new StreamResult(file); transformer.transform(source, result); } catch (TransformerException e) { e.printStackTrace(); } }下边的是xml文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="largeParameter" class="com.alibaba.omeo.captcha.service.VariableParameter"> <property name="width" value="150"/> <property name="height" value="40"/> <property name="GMinFontSize" value="34"/> <property name="GMaxFontSize" value="34"/> <property name="SMinFontSize" value="32"/> <property name="SMaxFontSize" value="32"/> <property name="HMinFontSize" value="30"/> <property name="HMaxFontSize" value="30"/> <property name="overlapPixel" value="2"/> <property name="horizontalMargin" value="5"/> <property name="verticalMargin" value="5"/> <property name="radius" value="40"/> <property name="XAmplitude" value="5"/> <property name="YAmplitude" value="5"/> <property name="XWaveLength" value="20"/> <property name="YWaveLength" value="10"/> <property name="scale" value="40"/> <property name="amount" value="8"/> <property name="turbulence" value="8"/> <property name="time" value="90"/> </bean> <bean id="randomFontGenerator" class="com.alibaba.omeo.captcha.component.font.RandomFontGenerator"> <property name="fonts"> <list> <value>Bitstream Charter</value> <value>Century Schoolbook L</value> <value>Courier 10 Pitch</value> <value>Bitstream Vera Serif</value> <value>URW Bookman L</value> </list> </property> </bean> <bean id="randomRangeColorGenerator" class="com.alibaba.omeo.captcha.component.color.RandomRangeColorGenerator"> <property name="redComponentRange"> <list> <value>150</value> <value>255</value> </list> </property> <property name="greenComponentRange"> <list> <value>150</value> <value>255</value> </list> </property> <property name="blueComponentRange"> <list> <value>150</value> <value>255</value> </list> </property> <property name="alphaComponentRange"> <list> <value>150</value> <value>255</value> </list> </property> </bean> </beans>
希望对各位有帮助!
相关文章推荐
- java中利用dom4j对XML文档的创建、解析、查找、修改、保存等操作。
- java中利用dom4j对XML文档的创建、解析、查找、修改、保存等操作。
- JAVA中用DOM修改xml,并保存
- JAVA工具类( 17) ---properties文件动态修改并自动保存
- 用javascript读取xml,并进行修改xml数据,解决保存没有权限问题
- JAVA打包xml成zip保存在服务器任意位置,并能在页面进行点击下载
- Eclipse 修改jsp、XML、 java、 HTML 每行显示的字数
- java图片保存在xml里
- 【Java开发】--jdom解析xml-修改xml
- java实现从文件中读取信息保存在Map中,修改Map中的值,将Map 4000 更新到文件中
- java解析xml文档并保存到数据库
- java dom4j新增,删除,修改,生成,导出XML
- java 解析/操作 xml 几种常用方式 xml的增加/删除/修改
- java非web应用修改 properties/xml配置文件后,无需重启应用即可生效---自动加载
- python模块xml,xml.etree.ElementTree 节点的读取,查询,修改,保存,创建,删除等
- Java读取和修改XML示例
- java修改xml的指定属性
- Java xml节点属性的修改
- C#使用XmlDocument操作XML进行查询、增加、修改、删除、保存应用的实例
- XML 转 String 的一种方法 Java - 修改 0 次