Element getXML()
来源:互联网 发布:淘宝单品流量怎么看 编辑:程序博客网 时间:2024/06/10 18:56
package com.thinking.serializable;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Serializer;
/**
*对象数据生成xml
* @author zy
*
*/
public class Person {
//成员属性
private String first ,last;
//构造方法,初始化成员变量
public Person(String first,String last){
this.first = first;
this.last = last;
}
//定义一个解析XML的方法,返回element
public Element getXML(){
//父节点
Element person = new Element("person");
//子节点:名
Element firstName = new Element("first");
//保存实例化的属性
firstName.appendChild(first);
Element lastName = new Element("last");
lastName.appendChild(last);
//父节点添加子节点
person.appendChild(firstName);
person.appendChild(lastName);
//返回对象给调用者
return person;
}
//Constructor to restore a Person from an XML Element
public Person(Element person){
//父节点获取子节点“first”的值
first = person.getFirstChildElement("first").getValue();
last = person.getFirstChildElement("last").getValue();
}
//格式化,一个输入流,一个文档对象
public static void format(OutputStream os,Document doc) throws Exception{
//Make it human-readable;
Serializer serializer = new Serializer(os,"ISO-8859-1");
serializer.setIndent(2);
serializer.setMaxLength(60);
serializer.write(doc);
serializer.flush();
}
public static void main(String[] args) throws Exception {
List<Person> people = Arrays.asList(
//构造方法,创建对象
new Person("Dr. Bunsen","Honeydew"),
new Person("Gonzo","The Great"),
new Person("Phililp J.","Fry"));
System.out.println(people);
//设置根目录
Element root = new Element("people");
//遍历ArrayList数组集合中的对象数据
for (Person p : people)
//读取子节点,并调用getXML()解析
root.appendChild(p.getXML());
//解析后的对象数据创建文档结构
Document doc =new Document(root);
//格式化文档视图
format(System.out,doc);
//创建xml文件名,并放到缓存流中,同时再格式化一次
format(new BufferedOutputStream(new FileOutputStream("People.xml")), doc);
}
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Serializer;
/**
*对象数据生成xml
* @author zy
*
*/
public class Person {
//成员属性
private String first ,last;
//构造方法,初始化成员变量
public Person(String first,String last){
this.first = first;
this.last = last;
}
//定义一个解析XML的方法,返回element
public Element getXML(){
//父节点
Element person = new Element("person");
//子节点:名
Element firstName = new Element("first");
//保存实例化的属性
firstName.appendChild(first);
Element lastName = new Element("last");
lastName.appendChild(last);
//父节点添加子节点
person.appendChild(firstName);
person.appendChild(lastName);
//返回对象给调用者
return person;
}
//Constructor to restore a Person from an XML Element
public Person(Element person){
//父节点获取子节点“first”的值
first = person.getFirstChildElement("first").getValue();
last = person.getFirstChildElement("last").getValue();
}
//格式化,一个输入流,一个文档对象
public static void format(OutputStream os,Document doc) throws Exception{
//Make it human-readable;
Serializer serializer = new Serializer(os,"ISO-8859-1");
serializer.setIndent(2);
serializer.setMaxLength(60);
serializer.write(doc);
serializer.flush();
}
public static void main(String[] args) throws Exception {
List<Person> people = Arrays.asList(
//构造方法,创建对象
new Person("Dr. Bunsen","Honeydew"),
new Person("Gonzo","The Great"),
new Person("Phililp J.","Fry"));
System.out.println(people);
//设置根目录
Element root = new Element("people");
//遍历ArrayList数组集合中的对象数据
for (Person p : people)
//读取子节点,并调用getXML()解析
root.appendChild(p.getXML());
//解析后的对象数据创建文档结构
Document doc =new Document(root);
//格式化文档视图
format(System.out,doc);
//创建xml文件名,并放到缓存流中,同时再格式化一次
format(new BufferedOutputStream(new FileOutputStream("People.xml")), doc);
}
}
www.gxnnfp.com
0 0
- Element getXML()
- getxml.java
- 分分钟getXML解析之DOM解析。
- Element
- Element
- element
- Framework 1.x 下的 DataSet.GetXml()问题,BUG?
- 客户端显示由WebService返回DataSet的GetXML()方法的返回值的方法
- jquery ---- getXml文件(遍历显示其中某个节点下的内容)
- element jquery
- element.childNodes
- dom element
- expat:element
- Element Object
- Element 对象
- prototype Element
- Remove Element
- Remove Element
- poj 3295 Tautology
- Greenplum优化--SQL调优篇
- 欢迎使用CSDN-markdown编辑器
- Data Structure:Binary Tree
- 图像处理之霍夫变换(直线检测算法)
- Element getXML()
- android ORM框架ORMLite封装
- 功率谱密度(功率信号)、能量谱密度(能量信号)详解
- MySql 申明变量以及赋值
- Trie树讲解
- AlertDialog以及子类
- 运维笔记3
- C++虚函数与纯虚函数的区别
- Codeforces Round #363 (Div. 2) D. Fix a Tree __ dfs+剪枝+标记数组