XML解析为对象
来源:互联网 发布:网络传销是怎么样的 编辑:程序博客网 时间:2024/04/28 14:50
jaxb解析xml为对象例子
文章分类:Java编程
通过jaxb方式把xml文件映射成bean对象。
1、新建java工程或者web工程都可以。
2、通过精简必须导入jar包
activation.jar
jaxb-api-2.0.jar
jaxb-impl-2.0.1.jar
jsr173_api-1.0.jar
3、 新建xml文件
<?xml version="1.0" encoding="UTF-8"?>
<root>
<template
start="true"
ip="127.0.0.1"
port="3344"
server="server"
/>
<template ... ... />
</root>
4、新建bean对象,xml的映射文件。
Vo.java 如下
该文件对应xml文件中 循环节点 template 元素
Root.java 文件如下
注意bean对象与xml映射的关系。
5、 解析xml为bean类
6、 把bean对象生成xml字符串方法
1、新建java工程或者web工程都可以。
2、通过精简必须导入jar包
activation.jar
jaxb-api-2.0.jar
jaxb-impl-2.0.1.jar
jsr173_api-1.0.jar
3、 新建xml文件
<?xml version="1.0" encoding="UTF-8"?>
<root>
<template
start="true"
ip="127.0.0.1"
port="3344"
server="server"
/>
<template ... ... />
</root>
4、新建bean对象,xml的映射文件。
Vo.java 如下
- import javax.xml.bind.annotation.XmlAccessType;
- import javax.xml.bind.annotation.XmlAccessorType;
- import javax.xml.bind.annotation.XmlAttribute;
- import javax.xml.bind.annotation.XmlRootElement;
- import javax.xml.bind.annotation.XmlType;
- @XmlAccessorType(XmlAccessType.FIELD)
- @XmlType(name = "", propOrder = {
- })
- @XmlRootElement(name = "[color=red]template[/color]")
- public class Vo {
- @XmlAttribute
- private String start;
- @XmlAttribute
- private String ip;
- @XmlAttribute
- private String port;
- @XmlAttribute
- private String server;
- public String getStart() {
- return start;
- }
- public void setStart(String start) {
- this.start = start;
- }
- public String getIp() {
- return ip;
- }
- public void setIp(String ip) {
- this.ip = ip;
- }
- public String getPort() {
- return port;
- }
- public void setPort(String port) {
- this.port = port;
- }
- public String getServer() {
- return server;
- }
- public void setServer(String server) {
- this.server = server;
- }
- }
该文件对应xml文件中 循环节点 template 元素
Root.java 文件如下
- import java.util.ArrayList;
- import java.util.List;
- import javax.xml.bind.annotation.XmlAccessType;
- import javax.xml.bind.annotation.XmlAccessorType;
- import javax.xml.bind.annotation.XmlRootElement;
- import javax.xml.bind.annotation.XmlType;
- @XmlAccessorType(XmlAccessType.FIELD)
- @XmlType(name = "", propOrder = {
- })
- @XmlRootElement(name = "[color=blue]root[/color]")
- public class Root {
- protected List<Vo> [color=red]template[/color];
- public List<Vo> getTemplateList() {
- if (template == null) {
- template = new ArrayList<Vo>();
- }
- return this.template;
- }
- }
注意bean对象与xml映射的关系。
5、 解析xml为bean类
- public static void gernateConfig() throws Exception{
- StringBuffer buffer = null;
- JAXBContext jaxbContext;
- try {
- //读入xml文件流
- InputStream is = Excecute.class.getResourceAsStream(“/conf/config.xml”);
- BufferedReader in = new BufferedReader(new InputStreamReader(is));
- buffer = new StringBuffer();
- String line = "";
- while ((line = in.readLine()) != null) {
- buffer.append(line);
- }
- //加载映射bean类
- jaxbContext = JAXBContext.newInstance(Root.class);
- //创建解析
- Unmarshaller um = jaxbContext.createUnmarshaller();
- StreamSource streamSource = new StreamSource(new StringReader(buffer.toString()));
- Root root = (Root) um.unmarshal(streamSource);
- } catch (Exception e) {
- e.printStackTrace();
- throw new Exception(e.getMessage());
- }
- }
6、 把bean对象生成xml字符串方法
- public static String gernateConfigXml(Root root) throws Exception {
- if (root != null) {
- try {
- JAXBContext context = JAXBContext.newInstance(Root.class);
- Marshaller m = context.createMarshaller();
- m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
- Writer writer = new StringWriter();
- m.marshal(root, writer);
- try {
- String xml = writer.toString();
- writer.flush();
- writer.close();
- return xml;
- } catch (IOException e) {
- return "";
- }
- } catch (Exception e) {
- throw new Exception("失败!");
- }
- } else {
- return null;
- }
- }
- XML解析为对象
- java解析XML为对象
- jaxb解析xml为对象例子
- nodejs解析xml字符串为对象
- java string类型xml解析为对象
- JavaScript加载XML文件,并解析为JSON对象
- 使用dom4j解析xml文件,并封装为javabean对象
- JAXB解析xml文件转换为Java对象
- XStream解析XML文本并用反射机制转换为对象
- XStream解析XML文本并用反射机制转换为对象
- 使用dom4j解析xml文件,并封装为javabean对象
- android XMl 解析神奇xstream 四: 将复杂的xml文件解析为对象
- js解析xml字符串或xml文件,将其转换为xml对象方法
- 使用dom4j 把xml格式解析为对象或者对象集合
- 后台解析XML为DataTable
- xml解析getElementById()为null
- 解析器把 XML 转换为 JavaScript 可存取的对象
- Xml文件转化为对象,对象转化为Xml
- 如何将全局程序集缓存GAC中的程序集导出来
- fw:第一人称:退休前我捐100万英镑
- Hello World
- spring - bean的生命周期
- 哈哈哈哈和
- XML解析为对象
- FlashPlayer debug 版本安装问题 汇总
- spring-依赖注入的方式
- jquery 特效
- Spring中的destroy-method
- Spring与Struts和JPA集成
- 认识理解Java中native方法
- JAVA学习暂时中断
- TechDiary_20110216