- 浏览: 468377 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (146)
- Maven (3)
- Quartz (10)
- Hessian (3)
- JDK (42)
- eclipse (4)
- 设计思想 (6)
- XML (8)
- JavaMail (1)
- Spring (11)
- mina (1)
- HsqlDb (1)
- Cache (2)
- Tool (6)
- 心情 (5)
- JQuery (0)
- Hadoop (5)
- Hbase (3)
- 自动构建 (7)
- JNDI (0)
- 代码赏析 (5)
- Oracle (1)
- Excel (4)
- Effective Java (5)
- JAXB (4)
- fdafasdf (1)
- ccc (0)
- web (3)
- concurrent (1)
- CVS (1)
- eclipse plugin (2)
- Apache (10)
最新评论
-
chxiaowu:
nice!
Quartz实现固定执行次数 -
zxjlwt:
学习了。http://surenpi.com
自定义ClassLoader -
kadlly:
public static final Logger log ...
Hessian 权限认证 -
spring_springmvc:
java程序语言学习教程 地址http://www.zuida ...
Java-Final -
liushuiwuyan:
[img][/img]
设计模式-单例
package ycl.learn.xml.jaxb; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; public class JAXBUtil<T> { @SuppressWarnings("unchecked") public T unmarshal(Class<T> clazz, InputStream is) throws JAXBException { JAXBContext context = JAXBContext.newInstance(clazz); Unmarshaller un = context.createUnmarshaller(); return (T) un.unmarshal(is); } public T unmarshal(Class<T> clazz, File file) throws JAXBException, FileNotFoundException { return unmarshal(clazz,new FileInputStream(file)); } public void marshal(T element,OutputStream os) throws JAXBException{ JAXBContext jc = JAXBContext.newInstance(element.getClass()); Marshaller m = jc.createMarshaller(); m.marshal(element, os); } public void marshal(T element, File output) throws FileNotFoundException, JAXBException{ marshal(element,new FileOutputStream(output)); } }
this is the simple util that packaging the JAXB operator.
then we can use it simplely.
1. we just export simple JODO to xml
package ycl.learn.xml.jaxb; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class EmployeeDO { private int id; private String gender; private int age; private String name; private String role; private String password; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getRole() { return role; } public void setRole(String role) { this.role = role; } @Override public String toString() { return "ID = " + id + " NAME=" + name + " AGE=" + age + " GENDER=" + gender + " ROLE=" + role + " PASSWORD=" + password; } }
we must add @XmlRootElement to point this object is the root object for export.
package ycl.learn.xml.jaxb; import java.io.File; import java.io.FileNotFoundException; import javax.xml.bind.JAXBException; public class JAXBUtilTest { private static final String FILE_NAME = "jaxb-emp-jaxbutil.xml"; /** * @param args * @throws JAXBException * @throws FileNotFoundException */ public static void main(String[] args) throws FileNotFoundException, JAXBException { // TODO Auto-generated method stub EmployeeDO emp = new EmployeeDO(); emp.setId(1); emp.setAge(25); emp.setName("Pankaj"); emp.setGender("Male"); emp.setRole("Developer"); emp.setPassword("sensitive"); JAXBUtil<EmployeeDO> ju = new JAXBUtil<EmployeeDO>(); ju.marshal(emp, new File(FILE_NAME)); System.out.println("generator success"); EmployeeDO empant = ju.unmarshal(EmployeeDO.class, new File(FILE_NAME)); System.out.println("reader success"); System.out.println(empant); } }
we can easily use JAXBUtil to marshal Object to file.
we can also easily use JAXBUtil to unmarchal File to Object.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <employeeDO> <age>25</age> <gender>Male</gender> <id>1</id> <name>Pankaj</name> <password>sensitive</password> <role>Developer</role> </employeeDO>
If you review this code carefully, you will find the default root element name is the Object's name with first letter is lowercase.
the element under the root element is the attribute of the object.
Q1: can i change the Element Name, to generator Xml
package ycl.learn.xml.jaxb; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name="content") public class EmployeeDO { private int id; private String gender; private int age; private String name; private String role; private String password; @XmlElement(name="pwd") public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getRole() { return role; } public void setRole(String role) { this.role = role; } @Override public String toString() { return "ID = " + id + " NAME=" + name + " AGE=" + age + " GENDER=" + gender + " ROLE=" + role + " PASSWORD=" + password; } }
we just need to set the @XmlRootElement or @XmlElement's name is ok.then the xml content is
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <content> <age>25</age> <gender>Male</gender> <id>1</id> <name>Pankaj</name> <pwd>sensitive</pwd> <role>Developer</role> </content>
Q2: can i point that some attribute of Object i don't want to export
package ycl.learn.xml.jaxb; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlTransient; @XmlRootElement(name="content") public class EmployeeDO { private int id; private String gender; private int age; private String name; private String role; private String password; @XmlElement(name="pwd") public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @XmlTransient public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getRole() { return role; } public void setRole(String role) { this.role = role; } @Override public String toString() { return "ID = " + id + " NAME=" + name + " AGE=" + age + " GENDER=" + gender + " ROLE=" + role + " PASSWORD=" + password; } }
you can add @XmlTransient before the getMethod, then this attribute won't be export.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <content> <age>25</age> <gender>Male</gender> <name>Pankaj</name> <pwd>sensitive</pwd> <role>Developer</role> </content>
Q3: can i export the attribute of Object as attribute, not Element of xml, and change the attribute name
@XmlAttribute(name="agefather") public int getAge() { return age; }
very easily to change sub element to attribute.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <content agefather="25"> <gender>Male</gender> <name>Pankaj</name> <pwd>sensitive</pwd> <role>Developer</role> </content>
Q4: i want to export the xml element as my order
@XmlRootElement(name="content") @XmlType(propOrder={"gender","age","name","role","password"}) public class EmployeeDO
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <content agefather="25"> <gender>Male</gender> <name>Pankaj</name> <role>Developer</role> <pwd>sensitive</pwd> </content>
you can see this element is ordered by our defined.
Q5: i want to export list of Objects
@XmlElementWrapper(name="employees") @XmlElement(name="employee") public List<Employee> getEmployees() { return employees; }
<employees> <employee> <age>25</age> <gender>GGGGGG</gender> <id>1</id> <name>Pankaj</name> <password>sensitive</password> <role>Developer</role> <family></family> </employee> <employee> <age>25</age> <gender>GGGGGGG</gender> <id>1</id> <name>Pankaj</name> <password>sensitive</password> <role>Developer</role> <family></family> </employee> </employees>
Q5: i want to export list of Objects in other Object
[ClassA] public Family getFamily() { return family; }
public class Family { private List<Employee> employees; @XmlElement(name="employee") public List<Employee> getEmployees() { return employees; } public void setEmployees(List<Employee> employees) { this.employees = employees; } }
<family> <employee> <age>25</age> <gender>LLLLLLL</gender> <id>1</id> <name>Pankaj</name> <password>sensitive</password> <role>Developer</role> <family></family> </employee> <employee> <age>25</age> <gender>Male</gender> <id>1</id> <name>Pankaj</name> <password>sensitive</password> <role>Developer</role> <family></family> </employee> </family>
oh, you just don't to defined family, because it is the element of the object, default define is ok.
the employee is multiple in family, you just define the @XmlElement empoyee is ok,and this will be auto add to list
so this is you can think of the four questions, and i can't think other situations, and i think this four points can resolve all problems that we encountered, we can easily change xml Element name, Element attribute, Element order, that's cool.
发表评论
-
JAXBのMap wih XmlAdapter
2013-12-11 12:49 1345We usually use List<Item> ... -
JAXBのXsd to Java Object.
2013-12-11 10:09 2211Xsd to java Object. right, use ... -
JAXBのReference with XmlAdapter
2013-12-11 09:51 1370sometimes i also doubt what Xml ... -
dom,sax,dom4j,jdom,xerces
2013-04-10 16:41 3053dom,sax,dom4j,jdom的关系就不描述了.xerc ... -
xStream之xml
2013-04-10 15:36 17401. 把对象进行字符串输出,把字符串作为对象读入 pack ... -
Spring的schemaResolver
2012-12-18 11:01 1629Spring解析xml可以参考以上,可以指定自定义的schem ... -
Sax解析Xml
2012-12-12 14:01 997对于解析大型的xml,可能使用整个document或整个文件都 ... -
Xml转化为Java,Java转化为Xml[JAXB]
2012-12-12 13:51 1942JAXB:这是java处理xml的标 ... -
Spring读取xml文件[schema/dtd]
2012-12-12 13:44 4899主测试类如下: 说明 使用了ErrorHandler,主要 ... -
Spring,Struts的DTD验证
2010-10-12 11:04 7693一般比较正式的XML信息中都会包含对应的DTD声明,用来定义 ...
相关推荐
Java Architecture for XML Binding (JAXB) 是Java平台中用于处理XML和Java对象之间转换的一个标准API。它使得在Java应用程序中使用...在实际项目中,结合JAXB与XML Schema,可以构建更健壮、易于维护的数据交换系统。
JAXB与其他XML处理技术(如DOM、SAX、StAX)相比,更注重于对象模型与XML之间的绑定,适用于需要频繁进行对象与XML转换的场景。而DOM适合处理小规模的XML文档,SAX和StAX则适用于大文件流式处理。 总结,JAXB是Java...
在Java开发中,JAXB(Java Architecture for XML Binding)是一个标准的API,用于将XML文档与Java对象之间进行互相转换。这个技术对于处理XML数据,尤其是解析和生成XML文档非常有用。当我们面临XML文档中存在嵌套子...
这个过程通常通过特定的API或库来实现,如JAXB(Java Architecture for XML Binding)或DOM(Document Object Model)。这些工具能够自动将Java对象的属性映射到XML元素和属性上,反之亦然。 Spring框架提供了一种...
总结来说,这个压缩包提供的内容涉及了Java中使用JAXB进行XML与Java对象之间转换的实际应用,以及辅助的DTO和文件读取工具。这些对于理解和实现XML数据处理的Java应用非常有帮助。开发者可以通过学习和使用这些示例...
在“XML动态转化为ObjectDemo”中,JAXB可能是实现XML到Object转换的主要工具。 3. **XML到Object转换步骤**: - 定义Java类:根据XML结构创建相应的Java类,每个元素对应类的一个字段,属性对应字段的注解。 - ...
在编程领域,Object与XML之间的转换是常见的数据处理任务,特别是在数据交换、持久化存储或者网络通信中。Object转XML和XML转Object的过程涉及到对象序列化和反序列化的概念。 对象序列化是将一个对象的状态转化...
JAXB可以基于XML Schema自动生成Java Bean类,或者将Java Bean类转换为XML Schema。 3. **Marshalling**: 将Java对象转换为XML的过程称为 marshalling。JAXB提供了`Marshaller`接口来实现这一功能。 4. **...
**JAXB(Java Architecture for XML Binding)** 是Java平台中用于处理XML的一种强大的工具,它提供了将XML文档与Java对象之间的映射,从而方便XML数据的解析和生成。通过JAXB,开发者可以轻松地实现XML数据到Java...
本文将详细介绍两种主要的XML与Java对象互相转换的方法:DOM(Document Object Model)解析和JAXB(Java Architecture for XML Binding)。 1. DOM解析: DOM是一种W3C标准,它将XML文档解析成一棵由节点组成的树,...
下面我们将详细探讨XML、Java与JSON的转换方法。 1. XML到JSON转换: - 使用Java的Jackson库:Jackson库中的`com.fasterxml.jackson.databind.ObjectMapper`类提供了将XML转换为JSON的功能。首先,我们需要解析XML...
通过在Java类上使用特定的注解,JAXB可以将Java对象转换为XML文档,反之亦然。 - **DOM(Document Object Model)**:DOM是一种将XML文档解析成树形结构的API,每个节点代表XML文档的一部分。Java程序员可以通过...
JAXB是Java SE的一部分,它提供了将Java对象直接转换为XML文档,以及从XML文档反序列化回Java对象的能力。首先,你需要为要转换的对象创建对应的Java类,并使用`@XmlRootElement`、`@XmlElement`等注解来指定XML...
本示例涉及的核心知识点是将Java中的List对象与XML文档进行相互转换。这里,我们使用的库是dom4j-1.6.1.jar,这是一个强大的Java XML处理库。 首先,让我们详细了解一下List和XML。List是Java集合框架的一部分,它...
5. **绑定上下文(Binder)**:`Binder`接口提供了在Java对象和XML DOM(Document Object Model)之间转换的能力,允许在处理XML时进行更灵活的操作。 6. **JAXB API**:`jaxb-api-2.2.6.jar`是JAXB的API库,其中...
在Java中处理XML,我们可以使用多种库和API,如JAXB (Java Architecture for XML Binding),DOM (Document Object Model),SAX (Simple API for XML) 和StAX (Streaming API for XML)。但根据提供的文件名"XStream-...
在Java中,JAXB(Java Architecture for XML Binding)和JDOM等库则简化了XML与Java对象之间的映射,使得数据交换更为便捷。此外,XSLT(Extensible Stylesheet Language Transformations)用于XML到XML的转换,而...
在Java开发中,JAXB(Java Architecture for XML Binding)是一个用于将XML文档和Java对象之间进行绑定的技术。在处理XML文件时,特别是涉及到序列化和反序列化时,可能会遇到字符编码问题,即所谓的“乱码”。这个...
5. **生成序列化和反序列化代码**:为了方便XML数据和Java对象之间的转换,工具还会生成解析XML和生成XML的代码,通常是基于JAXB(Java Architecture for XML Binding)或其他类似库。 除了XSD转Java,有些工具还...
总的来说,“XML_JAVA指南”应该涵盖了XML和Java之间的各种交互方式,包括解析、生成、验证和转换XML,以及使用JAXB进行对象-XML绑定。通过学习这些技术,开发者可以更好地在Java应用程序中集成和处理XML数据。