好友
阅读权限 10
听众
最后登录 1970-1-1
calf
发表于 2017-4-9 12:59
第一次发帖,有点小紧张(哈哈哈)
自己以前也没接触过xml最近是要完成上机作业才学习的,新手可以少走弯路! 希望可以帮到需要的同学们~~
没接触过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 [20];//用户
String [] mm = new String [20];//密码
String [] ye = new String [20];//余额
Integer [] integer = new Integer[20]; //创建一个整型数组来保存余额信息,因为要进行运算,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[i] = username.getTextContent();//用yh数组来接收username元素中的内容
mm[i] = passward.getTextContent();//用mm数组来接收passward元素中的内容
ye[i] = balance.getTextContent();//用ye数组来接收balance、元素中的内容
integer[i] = Integer.valueOf(ye[i]);//将string型数组ye转化成integer型数组,方便计算余额
System.out.println(yh[i]);
System.out.println(mm[i]);
System.out.println(ye[i]);
}
integer[0] = integer[0] + 200000;//对第一个元素进行加20000操作
ye[0] = String.valueOf(integer[0]);//再转化为string型
root.getElementsByTagName("balance").item(0).setTextContent(ye[0]);//将改变写进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[i] = username.getTextContent();
mm[i] = passward.getTextContent();
ye[i] = balance.getTextContent();
//integer[i] = Integer.valueOf(ye[i]);
System.out.println(yh[i]);
System.out.println(mm[i]);
System.out.println(ye[i]);
}
} 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();
}
}
}
-----------------------------------------------------------------
*****************************************
代码到此结束!
*****************************************
-----------------------------------------------------------------
免费评分
查看全部评分
发帖前要善用【论坛搜索 】 功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。