您的位置:首页 > 编程语言 > Java开发

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>


希望对各位有帮助!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: