最近在写ATM系统 分享一段关于后台xml数据读取和修改的代码
第一次发帖,有点小紧张(哈哈哈)自己以前也没接触过xml最近是要完成上机作业才学习的,新手可以少走弯路!{:301_1003:}希望可以帮到需要的同学们~~
没接触过xml的同学可以先去了解一下xml的结构!链接在此拿走不谢:http://www.w3school.com.cn/xml/index.asp
//passward打错了就没改过来 0.0应该是password 但是不影响 代码中附有很详细的注释---------------------------------------------------------------------------***********************************************下面是xml文件内容(文件名first.xml)***********************************************---------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<atm>
<user>
<username>apple</username>
<passward>100000</passward>
<balance>0</balance>
</user>
<user>
<username>boy</username>
<passward>200000</passward>
<balance>0</balance>
</user>
<user>
<username>client</username>
<passward>300000</passward>
<balance>0</balance>
</user>
<user>
<username>dog</username>
<passward>400000</passward>
<balance>0</balance>
</user>
<user>
<username>egg</username>
<passward>500000</passward>
<balance>0</balance>
</user>
<user>
<username>famle</username>
<passward>600000</passward>
<balance>0</balance>
</user>
<user>
<username>good</username>
<passward>700000</passward>
<balance>0</balance>
</user>
<user>
<username>high</username>
<passward>800000</passward>
<balance>0</balance>
</user>
<user>
<username>input</username>
<passward>900000</passward>
<balance>0</balance>
</user>
<user>
<username>job</username>
<passward>100111</passward>
<balance>0</balance>
</user>
</atm>
---------------------------------------------------------------------------
***********************************************
下面是java代码
***********************************************
---------------------------------------------------------------------------
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import javax.naming.spi.DirStateFactory.Result;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.dom4j.io.XMLWriter;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class testxml {
public static void main(String[] args) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File("first.xml")); //获得xml文件
Element root = document.getDocumentElement(); //获得xml文件的根元素
NodeList list = root.getElementsByTagName("user"); //获得根元素下面的子元素
//建立三个数组来保存读取到的三种内容
String [] yh = new String ;//用户
String [] mm = new String ;//密码
String [] ye = new String ;//余额
Integer [] integer = new Integer; //创建一个整型数组来保存余额信息,因为要进行运算,string型不能计算
for (int i = 0; i < list.getLength(); i++) {
Element user = (Element) list.item(i);//获取user的所有子元素的列表
System.out.println("----------------------");
Element username = (Element) user.getElementsByTagName("username").item(0);//获取username元素
Element passward = (Element) user.getElementsByTagName("passward").item(0);//获取passward元素
Element balance = (Element) user.getElementsByTagName("balance").item(0);//获取balance元素
yh = username.getTextContent();//用yh数组来接收username元素中的内容
mm = passward.getTextContent();//用mm数组来接收passward元素中的内容
ye = balance.getTextContent();//用ye数组来接收balance、元素中的内容
integer = Integer.valueOf(ye);//将string型数组ye转化成integer型数组,方便计算余额
System.out.println(yh);
System.out.println(mm);
System.out.println(ye);
}
integer = integer + 200000;//对第一个元素进行加20000操作
ye = String.valueOf(integer);//再转化为string型
root.getElementsByTagName("balance").item(0).setTextContent(ye);//将改变写进xml文件
//保存修改!!注意 没有保存的话 文件不会改变 这段代码弄了好久 很关键
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer1 = tFactory.newTransformer();
// transformer.setOutputProperty(OutputKeys.ENCODING, "GB2312");
DOMSource source1 = new DOMSource(document);
StreamResult result = new StreamResult(new File("first.xml"));
transformer1.transform(source1, result);
//再次循环打印出xml文件内容
for (int i = 0; i < list.getLength(); i++) {
Element user = (Element) list.item(i);
System.out.println("----------------------");
Element username = (Element) user.getElementsByTagName("username").item(0);
Element passward = (Element) user.getElementsByTagName("passward").item(0);
Element balance = (Element) user.getElementsByTagName("balance").item(0);
yh = username.getTextContent();
mm = passward.getTextContent();
ye = balance.getTextContent();
//integer = Integer.valueOf(ye);
System.out.println(yh);
System.out.println(mm);
System.out.println(ye);
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerFactoryConfigurationError e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
-----------------------------------------------------------------
*****************************************
代码到此结束!
*****************************************
----------------------------------------------------------------- 没有人我自己小顶一下 这个厉害呀 顶 学习一下!!!! 怎么回帖失败 a1s2d3 发表于 2017-4-9 16:30
怎么回帖失败
没有失败啊{:301_975:}{:301_975:} 多支持多顶一下。希望再次分享! 因素 发表于 2017-4-9 19:51
多支持多顶一下。希望再次分享!
谢谢支持{:301_975:} 进来学习一波,感谢分享~~支持! 感觉定义一个user对象去封装这三个属性可能会好点
页:
[1]
2