怎么用jaxb技术将xml转成javabean

 我来答
兄弟连教育
2016-07-05 · 百度知道合伙人官方认证企业
兄弟连教育
兄弟连教育成立于2006年,11年来专注IT职业教育,是国内专业的IT技术培训学校。2016年成功挂牌新三板(股票代码:839467)市值过亿。开设专注程序员培训专注php、Java、UI、云计算、Python、HTML5、
向TA提问
展开全部
  Java代码
  import java.util.ArrayList;
  import java.util.List;
  
  import javax.xml.bind.annotation.XmlAttribute;
  import javax.xml.bind.annotation.XmlElement;
  import javax.xml.bind.annotation.XmlRootElement;
  import javax.xml.bind.annotation.XmlTransient;
  
  @XmlRootElement(name="module")
  public class Module extends Metadata{
  
  @XmlAttribute(name="name")
  public String name;
  
  @XmlAttribute(name="template")
  public String template;
  
  @XmlElement
  public Resources resources;
  
  @XmlElement
  public HeaderItems headerItems;
  
  @XmlElement
  public Properties properties;
  
  @XmlElement
  public BodyItems bodyItems;
  
  @XmlTransient
  public String getName() {
  return name;
  }
  
  public void setName(String name) {
  this.name = name;
  }
  
  @XmlTransient
  public String getTemplate() {
  return template;
  }
  
  public void setTemplate(String template) {
  this.template = template;
  }
  
  @XmlTransient
  public Resources getResources() {
  return resources;
  }
  
  public void setResources(Resources resources) {
  this.resources = resources;
  }
  
  @XmlTransient
  public HeaderItems getHeaderItems() {
  return headerItems;
  }
  
  public void setHeaderItems(HeaderItems headerItems) {
  this.headerItems = headerItems;
  }
  
  @XmlTransient
  public Properties getProperties() {
  return properties;
  }
  
  public void setProperties(Properties properties) {
  this.properties = properties;
  }
  
  @XmlTransient
  public BodyItems getBodyItems() {
  return bodyItems;
  }
  
  public void setBodyItems(BodyItems bodyItems) {
  this.bodyItems = bodyItems;
  }
  }

Moduel对象其中一个对象属性:

  Java代码
  import java.util.List;
  
  import javax.xml.bind.annotation.XmlElement;
  import javax.xml.bind.annotation.XmlTransient;
  
  public class HeaderItems extends Metadata {
  
  @XmlElement(name="field")
  private List<Field> fields;
  
  @XmlTransient
  public List<Field> getFields() {
  return fields;
  }
  
  public void setFields(List<Field> fields) {
  this.fields = fields;
  for (int i=0; i<fields.size(); i++)
  fields.get(i).setHeaderItem(true);
  }
  
  public void clear() {
  fields.clear();
  }
  
  }

  Java代码
  package com.morningstar.wfe.metadata.node;
  
  import java.util.List;
  
  import javax.xml.bind.annotation.XmlAttribute;
  import javax.xml.bind.annotation.XmlElement;
  import javax.xml.bind.annotation.XmlTransient;
  
  public class Map extends Datasource {
  @XmlAttribute(name="name")
  public String name;
  
  @XmlAttribute(name="type")
  public String type;
  
  @XmlAttribute(name="url")
  public String url;
  
  @XmlElement
  public Entries entries;
  
  public void setName(String name) {
  this.name = name;
  }
  public void setType(String type) {
  this.type = type;
  }
  public void setEntries(Entries entries) {
  this.entries = entries;
  }
  
  @XmlTransient
  public String getName() {
  return name;
  }
  
  @XmlTransient
  public String getType() {
  return type;
  }
  
  @XmlTransient
  public Entries getEntries() {
  return this.entries;
  }
  
  @XmlTransient
  public String getUrl() {
  return url;
  }
  public void setUrl(String url) {
  this.url = url;
  }
  
  public int size() {
  if (entries == null) return 0;
  
  return entries.size();
  }
  
  public Entry get(int index) {
  if (entries == null) return null;
  
  return entries.get(index);
  }
  
  
  public List<Entry> getEntriesList() {
  return entries.getEntryList();
  }
  
  }

最后XML闪亮登场:

  Java代码
  <?xml version="1.0" encoding="UTF-8"?>
  
  <module name='showcase' template='showcase.ftl'>
  <resources>
  <map name='css' type='static'>
  <entries>
  <entry label="text_a1" value="text_a1"></entry>
  <entry label="text_a2" value="text_a2"></entry>
  <entry label="text_a3" value="text_a3"></entry>
  <entry label="text_c1" value="text_c1"></entry>
  <entry label="text_c2" value="text_c2"></entry>
  </entries>
  </map>
  <map name='status' type='ws' url="xxxx">
  </map>
  <map name='author' type='file' url="xxxx">
  </map>
  </resources>
  
  <headerItems>
  <field name='active1' type='checkbox' datasource='css'
  multiple='false' />
  <field name='title1' type='text' format="string" required="true"/>
  <field name='radio11' type='radio' datasource='css'/>
  <field name='url11' type='url' format="string"/>
  <field name='dropdownlist111' type='select' datasource='css' multiple="true" height="80" width="100"/>
  <field name='title22' type='textarea' width="200" height="100" spellCheck="true" format="string" required="true"/>
  </headerItems>
  
  
  <properties>
  <field name='active12' type='checkbox' datasource='status'
  multiple='false' />
  <field name='title12' type='date' format="int" />
  <field name='email11' type='email' visible="true" />
  
  </properties>
  
  <bodyItems>
  <field label="Name" name="name" type="text" visible="false"
  editable="false"></field>
  
  <field name="readings_list" type="datatable" action="insert,remove">
  <columns>
  <field label="Title" name="rSource" type="text" visible="true" width="30"/>
  <field label="URL" name="rURL" type="text" visible="true" width="40"/>
  <field label="Date" name="rDate" type="text" visible="true" width="50"/>
  <field label="Source" name="rSource" type="text" visible="true" width="60"/>
  </columns>
  </field>
  
  </bodyItems>
  
  </module>

Metadata是一个抽象类,描述了些 ID NAME VALUE基本属性 没什么特别的

关于XML转java对象的操作:

  Java代码
  public static Module parse(String metadata) throws MetadataParserException {
  Module module = null;
  try {
  JAXBContext jaxbContext = JAXBContext.newInstance(Module.class);
  Unmarshaller um = jaxbContext.createUnmarshaller();
  module = (Module)um.unmarshal(new ByteArrayInputStream(metadata.getBytes()));
  } catch (JAXBException e) {
  log.warn("JAXB castor failed to convert the metadata to module instance by {}",e.getMessage());
  throw new MetadataParserException();
  }
  return module;
  }

这样的话传进XML的字符串,就可以解析出这个JAVA对象。完全不用写任何过多的代码。岂不爽哉?!!!!

附一个java bean <=> xml 的unmi同志原创文档,主要是比较Jaxb和castor。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式