java xml jaxb_[Java]XML与JAXB

本文介绍了如何使用JAXB (Java Architecture for XML Binding) 将XML文件转换为Java类,并展示了Customer和Address类的详细结构和创建过程。通过实例演示了XML Schema的解析和对象的读写操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

a.xml

jinjihulu

suzhou

215200

Marshall

a.xsd

xjc生成java类

D:\JavaSoft\jdk6\bin>xjc -d f:/xmldemo -p demo.xml f:/xmldemo/a.xsd

parsing a schema...

compiling a schema...

demo\xml\Address.java

demo\xml\Customer.java

demo\xml\ObjectFactory.java

Customer.java

//

// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6

// See http://java.sun.com/xml/jaxb

// Any modifications to this file will be lost upon recompilation of the source schema.

// Generated on: 2011.12.30 at 04:13:13 PM CST

//

package demo.xml;

import javax.xml.bind.annotation.XmlAccessType;

import javax.xml.bind.annotation.XmlAccessorType;

import javax.xml.bind.annotation.XmlAttribute;

import javax.xml.bind.annotation.XmlElement;

import javax.xml.bind.annotation.XmlRootElement;

import javax.xml.bind.annotation.XmlType;

/**

*

Java class for Customer complex type.

*

*

The following schema fragment specifies the expected content contained within this class.

*

*

 
 

* <complexType name="Customer">

* <complexContent>

* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">

* <sequence>

* <element name="address" type="{}Address"/>

* <element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/>

* </sequence>

* <attribute name="id" type="{http://www.w3.org/2001/XMLSchema}string" />

* </restriction>

* </complexContent>

* </complexType>

*

*

*

*/

@XmlRootElement

@XmlAccessorType(XmlAccessType.FIELD)

@XmlType(name = "Customer", propOrder = {

"address",

"name"

})

public class Customer {

@XmlElement(required = true)

protected Address address;

@XmlElement(required = true)

protected String name;

@XmlAttribute

protected String id;

/**

* Gets the value of the address property.

*

* @return

* possible object is

* {@link Address }

*

*/

public Address getAddress() {

return address;

}

/**

* Sets the value of the address property.

*

* @param value

* allowed object is

* {@link Address }

*

*/

public void setAddress(Address value) {

this.address = value;

}

/**

* Gets the value of the name property.

*

* @return

* possible object is

* {@link String }

*

*/

public String getName() {

return name;

}

/**

* Sets the value of the name property.

*

* @param value

* allowed object is

* {@link String }

*

*/

public void setName(String value) {

this.name = value;

}

/**

* Gets the value of the id property.

*

* @return

* possible object is

* {@link String }

*

*/

public String getId() {

return id;

}

/**

* Sets the value of the id property.

*

* @param value

* allowed object is

* {@link String }

*

*/

public void setId(String value) {

this.id = value;

}

}

Address.java

//

// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6

// See http://java.sun.com/xml/jaxb

// Any modifications to this file will be lost upon recompilation of the source schema.

// Generated on: 2011.12.30 at 04:13:13 PM CST

//

package demo.xml;

import javax.xml.bind.annotation.XmlAccessType;

import javax.xml.bind.annotation.XmlAccessorType;

import javax.xml.bind.annotation.XmlElement;

import javax.xml.bind.annotation.XmlType;

/**

*

Java class for Address complex type.

*

*

The following schema fragment specifies the expected content contained within this class.

*

*

 
 

* <complexType name="Address">

* <complexContent>

* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">

* <sequence>

* <element name="street" type="{http://www.w3.org/2001/XMLSchema}string"/>

* <element name="city" type="{http://www.w3.org/2001/XMLSchema}string"/>

* <element name="zip" type="{}ZipCodeType"/>

* </sequence>

* </restriction>

* </complexContent>

* </complexType>

*

*

*

*/

@XmlAccessorType(XmlAccessType.FIELD)

@XmlType(name = "Address", propOrder = {

"street",

"city",

"zip"

})

public class Address {

@XmlElement(required = true)

protected String street;

@XmlElement(required = true)

protected String city;

protected int zip;

/**

* Gets the value of the street property.

*

* @return

* possible object is

* {@link String }

*

*/

public String getStreet() {

return street;

}

/**

* Sets the value of the street property.

*

* @param value

* allowed object is

* {@link String }

*

*/

public void setStreet(String value) {

this.street = value;

}

/**

* Gets the value of the city property.

*

* @return

* possible object is

* {@link String }

*

*/

public String getCity() {

return city;

}

/**

* Sets the value of the city property.

*

* @param value

* allowed object is

* {@link String }

*

*/

public void setCity(String value) {

this.city = value;

}

/**

* Gets the value of the zip property.

*

*/

public int getZip() {

return zip;

}

/**

* Sets the value of the zip property.

*

*/

public void setZip(int value) {

this.zip = value;

}

}

ObjectFactory.java

//

// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6

// See http://java.sun.com/xml/jaxb

// Any modifications to this file will be lost upon recompilation of the source schema.

// Generated on: 2011.12.30 at 04:13:13 PM CST

//

package demo.xml;

import javax.xml.bind.JAXBElement;

import javax.xml.bind.annotation.XmlElementDecl;

import javax.xml.bind.annotation.XmlRegistry;

import javax.xml.namespace.QName;

/**

* This object contains factory methods for each

* Java content interface and Java element interface

* generated in the generated package.

*

An ObjectFactory allows you to programatically

* construct new instances of the Java representation

* for XML content. The Java representation of XML

* content can consist of schema derived interfaces

* and classes representing the binding of schema

* type definitions, element declarations and model

* groups. Factory methods for each of these are

* provided in this class.

*

*/

@XmlRegistry

public class ObjectFactory {

private final static QName _Address_QNAME = new QName("", "address");

private final static QName _Customer_QNAME = new QName("", "customer");

/**

* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: generated

*

*/

public ObjectFactory() {

}

/**

* Create an instance of {@link Customer }

*

*/

public Customer createCustomer() {

return new Customer();

}

/**

* Create an instance of {@link Address }

*

*/

public Address createAddress() {

return new Address();

}

/**

* Create an instance of {@link JAXBElement }{@code }}

*

*/

@XmlElementDecl(namespace = "", name = "address")

public JAXBElement

createAddress(Address value) {

return new JAXBElement

(_Address_QNAME, Address.class, null, value);

}

/**

* Create an instance of {@link JAXBElement }{@code }}

*

*/

@XmlElementDecl(namespace = "", name = "customer")

public JAXBElement createCustomer(Customer value) {

return new JAXBElement(_Customer_QNAME, Customer.class, null, value);

}

}

JaxbDemo.java

package demo.xml;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.OutputStream;

import javax.xml.bind.JAXBContext;

import javax.xml.bind.JAXBElement;

import javax.xml.bind.Marshaller;

import javax.xml.bind.Unmarshaller;

public class JaxbDemo {

public static void main(String[] args) throws Exception {

unmarshalling();

marshalling();

}

static void unmarshalling() throws Exception{

JAXBContext jc = JAXBContext.newInstance("demo.xml");

Unmarshaller u = jc.createUnmarshaller();

JAXBElement customerE = (JAXBElement) u.unmarshal(new FileInputStream(

"F:\\xmldemo\\a.xml"));

Customer bo = (Customer) customerE.getValue();

System.out.println(bo.getName() + " : " + bo.getAddress().getCity() + " : " + bo.getId());

}

static void marshalling()throws Exception{

JAXBContext jc = JAXBContext.newInstance(Customer.class);

Marshaller ms = jc.createMarshaller();

ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

Customer cust = new Customer();

cust.setId("cust id");

cust.setName("Marshall");

Address a = new Address();

a.setCity("suzhou");

a.setStreet("jinjihulu");

a.setZip(215200);

cust.setAddress(a);

OutputStream os = new FileOutputStream("F:\\xmldemo\\a.xml");

ms.marshal(cust, os);

os.close();

}

}

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2011-12-30 16:50

浏览 7266

评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值