- 浏览: 118681 次
- 性别:
- 来自: 南宁
-
文章分类
最新评论
我是要把一个JAVABEAN写入一个XML中,然后再度出,我是利用了一个vector,先把所有要读入的同一个StudentBean都添加到一个vector中我的StudentBean如下:
import java.io.Serializable;
public class StudentBean implements Serializable
{
String name;
String sex;
String phone;
public StudentBean(String name, String sex, String phone) {
super();
this.name = name;
this.sex = sex;
this.phone = phone;
}
public StudentBean()
{};
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
写操作是如下:
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.util.Vector;
public class writexml {
private Document document;
private String filename;
public writexml(String name) throws ParserConfigurationException{
filename=name;
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
document=builder.newDocument();
}
public void toWrite(Vector<StudentBean> stu_vector)
{
Element root = document.createElement("seller_record");
document.appendChild(root);
for(int i = 0;i<stu_vector.size();i++)
{
StudentBean stu = (StudentBean) stu_vector.get(i);
Element student = document.createElement("student");
student.setAttribute("name",stu.name);
root.appendChild(student);
Element sex = document.createElement("sex");
student.appendChild(sex);
Text tsex = document.createTextNode(stu.sex);
sex.appendChild(tsex);
Element phone = document.createElement("phone");
student.appendChild(phone);
Text tphone = document.createTextNode(stu.phone);
phone.appendChild(tphone);
}
}
public void toSave(){
try{
TransformerFactory tf=TransformerFactory.newInstance();
Transformer transformer=tf.newTransformer();
DOMSource source=new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING,"GB2312");
transformer.setOutputProperty(OutputKeys.INDENT,"yes");
PrintWriter pw=new PrintWriter(new FileOutputStream(filename));
StreamResult result=new StreamResult(pw);
transformer.transform(source,result);
}
catch(TransformerException mye){
mye.printStackTrace();
}
catch(IOException exp){
exp.printStackTrace();
}
}
public static void main(String args[]){
Vector<StudentBean> stu_vector = new Vector<StudentBean>();
StudentBean stu = new StudentBean("姚明","男","123456");
stu_vector.add(stu);
stu = new StudentBean("刘翔","女","0988556");
stu_vector.add(stu);
try{
writexml myxml=new writexml("E:\\软件\\eclipse\\workstation\\IOxml\\src\\seller.xml");
myxml.toWrite(stu_vector);
myxml.toSave();
System.out.print("Your writing is successful.");
}
catch(ParserConfigurationException exp){
exp.printStackTrace();
System.out.print("Your writing is failed.");
}
}
}
下面是读操作:
import java.io.*;
import java.util.Vector;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class readxml {
static Document document;
private boolean validating;
public readxml() {
}
public Vector<StudentBean> toRead(String filename)
{
Vector<StudentBean> stu_vector=new Vector<StudentBean>();
StudentBean stu = new StudentBean();
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringElementContentWhitespace(true);
factory.setValidating(validating);
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(new File(filename));
// document.getDocumentElement().normalize();//注释(0)
Element root = document.getDocumentElement();
NamedNodeMap attr;
NodeList children = root.getElementsByTagName("student");//注释(1)
attr = root.getAttributes();
if(attr!=null)
{
for(int i = 0;i<children.getLength();i++)
{
Element element= (Element) children.item(i);
stu.setName(element.getAttribute("name"));
NodeList list = element.getChildNodes();
System.out.println(list.getLength());
Node child = list.item(i);
System.out.println(element.getAttribute("name"));
System.out.println(list.item(1).getTextContent());//注释(2)
System.out.println(list.item(3).getTextContent());//注释(3)
stu.setName(element.getAttribute("name"));
stu.setSex(list.item(1).getTextContent());
stu.setPhone(list.item(3).getTextContent());
stu_vector.add(stu);
}
}
}
catch (Exception exp)
{
exp.printStackTrace();
return null;
}
return stu_vector;
}
public static void main(String[] args) {
Vector<StudentBean> A = null;
readxml my = new readxml();
A = my.toRead("E:\\软件\\eclipse\\workstation\\IOxml\\src\\seller.xml");
System.out.println(A.size());
System.out.println("asd");
}
}
在下面是我读入的XML格式如下:
<seller_record>
−
<student name="姚明">
<sex>男</sex>
<phone>123456</phone>
</student>
−
<student name="刘翔">
<sex>女</sex>
<phone>0988556</phone>
</student>
</seller_record>
我写入的XML文档根有两个孩子,两个孩子又有3个node;
下面简要谈一下我对读操作的理解(主要是我标的注释的地方):
注释(1):以tag为student为标志建立NodeList,其长度为2,这个是符合逻辑的,但是下面的children的长度老提示是5,开始时令我很郁闷,后来查书和上网看了一下,才知道原来
<student name="姚明">
<sex>男</sex>他们两个之间是有空格的,他们之间的空格也算一个Node,所以才形成了注释(2)(3)的item标号为1,3。书上说有办法吧他们之间的空格去掉,其给的一个办法是用以下方法:
for(int i=0;i<list.getLength();i++)
{
Node child = list.item(i);
if(child instanceof Element)
{
Element childElement = (Element)child;
System.out.println(childElement.getTextContent());
}
}
但我认为是没必要,因为它实际上是把空格去掉或者再添加到一个NodeList中,我认为完全没有这个必要,还有另一个方法就是再写入的时候注明文档的文档类型,其实我不知道注释(0)的作用,一开始感觉应该跟刚才的空格问题有关,但是后来试了一下发现有没有这行代码都没有什么影响,不知道为什么。如果大家看了还有什么不明白的地方可以共同探讨一下,我也还有很多要学习的地方。
import java.io.Serializable;
public class StudentBean implements Serializable
{
String name;
String sex;
String phone;
public StudentBean(String name, String sex, String phone) {
super();
this.name = name;
this.sex = sex;
this.phone = phone;
}
public StudentBean()
{};
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
写操作是如下:
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.util.Vector;
public class writexml {
private Document document;
private String filename;
public writexml(String name) throws ParserConfigurationException{
filename=name;
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
document=builder.newDocument();
}
public void toWrite(Vector<StudentBean> stu_vector)
{
Element root = document.createElement("seller_record");
document.appendChild(root);
for(int i = 0;i<stu_vector.size();i++)
{
StudentBean stu = (StudentBean) stu_vector.get(i);
Element student = document.createElement("student");
student.setAttribute("name",stu.name);
root.appendChild(student);
Element sex = document.createElement("sex");
student.appendChild(sex);
Text tsex = document.createTextNode(stu.sex);
sex.appendChild(tsex);
Element phone = document.createElement("phone");
student.appendChild(phone);
Text tphone = document.createTextNode(stu.phone);
phone.appendChild(tphone);
}
}
public void toSave(){
try{
TransformerFactory tf=TransformerFactory.newInstance();
Transformer transformer=tf.newTransformer();
DOMSource source=new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING,"GB2312");
transformer.setOutputProperty(OutputKeys.INDENT,"yes");
PrintWriter pw=new PrintWriter(new FileOutputStream(filename));
StreamResult result=new StreamResult(pw);
transformer.transform(source,result);
}
catch(TransformerException mye){
mye.printStackTrace();
}
catch(IOException exp){
exp.printStackTrace();
}
}
public static void main(String args[]){
Vector<StudentBean> stu_vector = new Vector<StudentBean>();
StudentBean stu = new StudentBean("姚明","男","123456");
stu_vector.add(stu);
stu = new StudentBean("刘翔","女","0988556");
stu_vector.add(stu);
try{
writexml myxml=new writexml("E:\\软件\\eclipse\\workstation\\IOxml\\src\\seller.xml");
myxml.toWrite(stu_vector);
myxml.toSave();
System.out.print("Your writing is successful.");
}
catch(ParserConfigurationException exp){
exp.printStackTrace();
System.out.print("Your writing is failed.");
}
}
}
下面是读操作:
import java.io.*;
import java.util.Vector;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class readxml {
static Document document;
private boolean validating;
public readxml() {
}
public Vector<StudentBean> toRead(String filename)
{
Vector<StudentBean> stu_vector=new Vector<StudentBean>();
StudentBean stu = new StudentBean();
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringElementContentWhitespace(true);
factory.setValidating(validating);
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(new File(filename));
// document.getDocumentElement().normalize();//注释(0)
Element root = document.getDocumentElement();
NamedNodeMap attr;
NodeList children = root.getElementsByTagName("student");//注释(1)
attr = root.getAttributes();
if(attr!=null)
{
for(int i = 0;i<children.getLength();i++)
{
Element element= (Element) children.item(i);
stu.setName(element.getAttribute("name"));
NodeList list = element.getChildNodes();
System.out.println(list.getLength());
Node child = list.item(i);
System.out.println(element.getAttribute("name"));
System.out.println(list.item(1).getTextContent());//注释(2)
System.out.println(list.item(3).getTextContent());//注释(3)
stu.setName(element.getAttribute("name"));
stu.setSex(list.item(1).getTextContent());
stu.setPhone(list.item(3).getTextContent());
stu_vector.add(stu);
}
}
}
catch (Exception exp)
{
exp.printStackTrace();
return null;
}
return stu_vector;
}
public static void main(String[] args) {
Vector<StudentBean> A = null;
readxml my = new readxml();
A = my.toRead("E:\\软件\\eclipse\\workstation\\IOxml\\src\\seller.xml");
System.out.println(A.size());
System.out.println("asd");
}
}
在下面是我读入的XML格式如下:
<seller_record>
−
<student name="姚明">
<sex>男</sex>
<phone>123456</phone>
</student>
−
<student name="刘翔">
<sex>女</sex>
<phone>0988556</phone>
</student>
</seller_record>
我写入的XML文档根有两个孩子,两个孩子又有3个node;
下面简要谈一下我对读操作的理解(主要是我标的注释的地方):
注释(1):以tag为student为标志建立NodeList,其长度为2,这个是符合逻辑的,但是下面的children的长度老提示是5,开始时令我很郁闷,后来查书和上网看了一下,才知道原来
<student name="姚明">
<sex>男</sex>他们两个之间是有空格的,他们之间的空格也算一个Node,所以才形成了注释(2)(3)的item标号为1,3。书上说有办法吧他们之间的空格去掉,其给的一个办法是用以下方法:
for(int i=0;i<list.getLength();i++)
{
Node child = list.item(i);
if(child instanceof Element)
{
Element childElement = (Element)child;
System.out.println(childElement.getTextContent());
}
}
但我认为是没必要,因为它实际上是把空格去掉或者再添加到一个NodeList中,我认为完全没有这个必要,还有另一个方法就是再写入的时候注明文档的文档类型,其实我不知道注释(0)的作用,一开始感觉应该跟刚才的空格问题有关,但是后来试了一下发现有没有这行代码都没有什么影响,不知道为什么。如果大家看了还有什么不明白的地方可以共同探讨一下,我也还有很多要学习的地方。
发表评论
-
android 横屏重启的解决方案
2011-09-19 15:45 1557Android每次屏幕的横竖切换Activity会重新OnCr ... -
java 类的序列化
2011-08-30 11:57 811序列化: 可以将一个对象保存到一个文件,所以可以通过流的方式 ... -
android全屏显示的方法
2011-08-15 11:16 1441我们在开发程序是经常会需要软件全屏显示、自定义标题(使用按钮等 ... -
模仿AJAX上传文件
2011-07-27 11:08 0首先要说的就是 ajax 是无法实现上传文件的,可以想一下aj ... -
模仿AJAX上传文件
2011-07-27 11:08 641首先要说的就是 ajax 是无法实现上传文件的,可以想一下aj ... -
模仿AJAX上传文件
2011-07-27 11:07 686首先要说的就是 ajax 是无法实现上传文件的,可以想一下aj ... -
模仿AJAX上传文件
2011-07-27 11:07 883首先要说的就是 ajax 是无法实现上传文件的,可以想一下aj ... -
java链接数据库乱码解决方案
2011-05-20 17:56 1254Java乱码问题解决方 ... -
java 连接数据库实例
2011-03-16 11:43 986package com.dao;import java.sql ... -
java html标签过滤类
2011-02-12 00:24 1199import java.util.regex.Matcher; ... -
java 批量下载图片,批量打包文件并下载
2011-02-11 14:15 17001、批量下载网页上,动态生成的图片到服务器上指定的目录中 2、 ... -
myeclipse 注册的方法
2011-02-11 00:42 676import java.io.BufferedReader;i ... -
利用java.net包访问网络,连接网页并返回html页面代码
2011-02-11 00:28 1420package org.kevin;import java.n ... -
Java清除HTML标签
2011-02-11 00:14 1546使用正则表达式删除HTML标签。 import ja ... -
j2me 环境搭建
2011-01-17 09:17 832用Eclipse和WTK搭建J2ME开发环境 关键字: j2 ... -
jdbc 连接sql2008的问题解决
2010-12-31 16:24 1549首先下JDBC4,然后解压在C ... -
Struts 开发帮助文件
2010-12-25 23:58 582一、 概述struts是基于当前的一些相关标准的开发技术 ... -
java给图片加图片水印
2010-12-08 21:00 776package com.test;import java.aw ... -
java连接数据库大全
2010-11-06 10:30 755JAVA连接数据库大全 关键字: 数据库连接大全 J ... -
两个数组求并集
2010-10-26 22:59 1876对两个数组求并集,由于对数组的操作不是很熟悉,所以将数组中的数 ...
相关推荐
Find My iPhone
iphone底座第2级!!! DIY自己的iPhone底座!!!
详细介绍了iPhone的Find my iphone定位的七种功能,教你轻松找回丢失的iPhone手机
iPhone4各版本shsh
iOS 11.4(15F79) (15F5037c)真机包,不需要在更新xcode,直接下载后在访达前往 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport 然后载入就行。
iphone4原厂完整电路图PCB.苹果4图纸
iphone4越狱软件iphone4越狱软件iphone4越狱软件iphone4越狱软件iphone4越狱软件iphone4越狱软件
iphone 4 schematic and service manual
iPhone4 3.1 6.0.1.shsh
iPhone4 english introduction iPhone4 english introduction
https://github.com/kamyarelyasi/Libarclite-Files中的libarclite_iphoneos.a
iPhone4各种SHSH 欢迎下载
iphone3,1 shsh iphone4 5.1.1iphone3,1 shsh iphone4 5.1.1
Phone4 8G版 A1332 IOS5.11SHSH 能通过苹果验证
iphone4原理图 !设计参考
适配机型:iPhone5 A1429 (二网无锁gsm版或三网无锁CDMA版)理论支持iPhone5S、5C的三网无锁版 系统:能越狱的系统,实测:7.0.3、7.0.6、7.1.2 方法:1、越狱; 2、如果要使用3G/4G网络,越狱后,添加源:cydia...
Raspberry PI FindMyIPhone 是一个简单的脚本,它将登录到 iCloud 并触发“查找我的 iPhone”功能以使您的 iDevice 播放声音。 目前,它使用 GPIO 23 作为 LED 输出和 GPIO 24 作为按钮输入(低电平有效)。 也许...
iphone5 越狱教程 iphone5 越狱 详细的越狱教程一步步教你怎么越狱!
iphone4/iphone4s无信号解决方法
iPhone 4解锁教程iPhone 4解锁教程