原文地址: http://kushanxp.blogspot.com/2011/03/hello-world-with-java-architecture-for.html
What is JAXB?
JAXB (Java Architecture for XML Binding) facilitates you to read or write to a XML file. since XML involves with Java everywhere, It will be very useful when working with Java. Here I'm assuming you are having some knowledge on XML.
What you should have
1. JAXB
Download it and add its bin folder to your classpath.
I have used eclipse as the IDE.
Let's start...
1. Create a project in eclipse called JAXB.
2. Create the library directory and copy all the JAXB jars there. (lets call its "lib")
3. Add lib to the build path.
4. You should have a XML schema definition file, so let's create one. Let's name it as GSCProfile.xsd
5. Copy following content to the schema file.
GSCProfile.xsd
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/GSCProfile"
xmlns:tns="http://www.example.org/GSCProfile" elementFormDefault="qualified">
<complexType name="GSC">
<sequence>
<element name="Name" type="string"></element>
<element name="Rating" type="int"></element>
</sequence>
</complexType>
<complexType name="Profile">
<sequence>
<element name="ProfileName" type="string"></element>
<element name="GSCElements" type="tns:GSC" maxOccurs="14" minOccurs="14">
</element>
</sequence>
</complexType>
<complexType name="GSCProfiles">
<sequence>
<element name="Profile" type="tns:Profile" maxOccurs="unbounded" minOccurs="1">
</element>
</sequence>
</complexType>
</schema>
There are 3 complex types called,
1.GSCProfiles
2.Profile
3.GSC
GSCProfiles is the root of all. here how is the relationship goes,
GSCProfiles ---< Profile ---< GSC
Once you have added the bin folder to the classpath, go to the src folder of your project from command line and issue the following command,
xjc -d binding -p com.jaxb.gsc GSCProfiles.xsd
This will generates several classes in the above mentioned package (com.jaxb.gsc) one class for each complex type and an object factory and a package info class.
create another class called JAXBTest in a diffrernt package, lets name that as com.jaxb.test
JAXBTest.java
Create this variable at class level. and variable to hold the xml file name.
private static Map<String, Map<String, String>> gscProfileMap = new HashMap<String, Map<String, String>>();
/**
* Output xml file name.
*/
private static File xmlFile = new File("xml/GSCProfile.xml");
1. create a method called saveGSCProfiles and add following code fragments there. this method will save the content for it.
public static void saveGSCProfiles() {
try {
Map<String, String> gscValueMap = new HashMap<String, String>();
// Adding GSC Elements
gscValueMap.put("Data Communications", "0");
gscValueMap.put("Distributed Data Processing", "0");
gscValueMap.put("Performance", "0");
gscValueMap.put("Heavily Used Configuration", "0");
gscValueMap.put("Transaction Rate", "0");
gscValueMap.put("Online Data Entry", "0");
gscValueMap.put("End User Efficiency", "0");
gscValueMap.put("Online Update", "0");
gscValueMap.put("Complex Processing", "0");
gscValueMap.put("Reusability", "0");
gscValueMap.put("Installation Ease", "0");
gscValueMap.put("Operational Ease", "0");
gscValueMap.put("Multiple Sites", "0");
gscValueMap.put("Facilitate Change", "0");
gscProfileMap.put("ProfileName", gscValueMap);
Set<String> pofileNames = gscProfileMap.keySet();
ObjectFactory factory = new ObjectFactory();
GSCProfiles gscProfiles = factory.createGSCProfiles();
for (String profileKey : pofileNames) {
Profile profile = factory.createProfile();
// Setting profile name.
profile.setProfileName(profileKey);
gscValueMap = gscProfileMap.get(profileKey);
Set<String> gscSet = gscValueMap.keySet();
for (String gscKey : gscSet) {
GSC gsc = factory.createGSC();
gsc.setName(gscKey);
gsc.setRating(Integer.parseInt(gscValueMap.get(gscKey)));
profile.getGSCElements().add(gsc);
}
gscProfiles.getProfile().add(profile);
}
JAXBContext jaxbContext = JAXBContext.newInstance("com.jaxb.gsc");
// Creating marshaller.
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
FileOutputStream outputStream = new FileOutputStream(xmlFile);
// Marshalling object in to the XML file.
marshaller.marshal(gscProfiles, outputStream);
} catch (JAXBException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
2. Create another method to read from the XML file. let's name this as loadGSCProfiles
public static void loadGSCProfiles() {
try {
JAXBContext jaxbContext = JAXBContext.newInstance("com.jaxb.gsc");
// Create unmarshaller.
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
GSCProfiles gscProfiles = (GSCProfiles) unmarshaller.unmarshal(xmlFile);
// Retrieving list of profiles.
List<Profile> profile = gscProfiles.getProfile();
for (Profile profile2 : profile) {
Map<String, String> gscValueMap = new HashMap<String, String>();
List<GSC> gsc = profile2.getGSCElements();
for (GSC gsc2 : gsc) {
gscValueMap.put(gsc2.getName(), gsc2.getRating() + "");
}
gscProfileMap.put(profile2.getProfileName(), gscValueMap);
}
} catch (JAXBException e) {
e.printStackTrace();
}
Create a directory inside your eclipse project and name it as xml, and create a xml file called GSCProfile.xml
Write the main method in JAXBTest class and execute,
public static void main(String[] args) {
loadGSCProfiles();
saveGSCProfiles();
}
Open your GSCProfile.xml file and see...
Finally your eclipse project directory structure should like this:
[img]
https://lh3.googleusercontent.com/-zXTWw-VJ8ts/TWyDmdt94WI/AAAAAAAAAr8/DdkG38dtDuc/s1600/jaxb.PNG
[/img]
一下为评论:
Blaise Doughan said...
Hi Kushan,
You should check out the JAXB tooling being done for Eclipse by the Dali team:
- http://www.eclipse.org/webtools/releases/3.2.0/NewAndNoteworthy/jpa.php
-Blaise
March 2, 2011 7:57 PM
Kushan Jayathilake said...
Hi Blaise,
That's a great tool, I will definitely use it hereafter, Thank you very much for the info..
March 2, 2011 9:23 PM
分享到:
相关推荐
**JAXB(Java Architecture for XML Binding)** 是Java平台上的一个标准技术,用于将Java对象绑定到XML(eXtensible Markup Language)文档,以及从XML数据还原为Java对象。它是Java SE和Java EE的一部分,提供了方便...
在提供的文件名"jaxb-binding-date.xml"中,我们可以推测这是CXF和JAXB(Java Architecture for XML Binding)相关的配置,用于定制XML数据绑定规则。JAXB是Java中用于将XML数据转换为Java对象,反之亦然的工具。在...
在JAVA6中,JAXB(Java Architecture for XML Binding)和SAAJ(SOAP with Attachments API for Java)是JAX-WS的重要组成部分。JAXB用于XML和Java对象之间的自动绑定,而SAAJ则处理SOAP消息的创建和解析。 六、...
在实际应用中,我们可能还需要处理数据绑定,如XML到Java对象的转换,这可以通过JAXB(Java Architecture for XML Binding)实现。JAXB提供了一种自动化的机制,将Java对象序列化为XML,反之亦然。 此外,为了测试...
- **JAX-WS 2.0 (JSR224)** 是一个完全基于标准的实现,它使用了 Java Architecture for XML Binding (JAXB, JSR222) 在绑定层,并使用 Streaming API for XML (StAX, JSR173) 在解析层。同时,它完全支持 schema ...
- 核心库更新:包括JEP(Java Enhancement Proposal)306的文本块改进,以及对JAXB(Java Architecture for XML Binding)的升级。 - 性能优化:通过JEP 389,Java-17改进了垃圾回收器,提高了内存管理效率。 - ...
首先,XFire基于Java语言,利用了Java API for XML Processing (JAXP) 和Java Architecture for XML Binding (JAXB) 这些核心技术。JAXP用于XML文档的解析和生成,而JAXB则负责Java对象与XML之间的映射,使得处理XML...
涉及的jar包可能包括JAX-WS的API(如jaxws-api.jar)和实现(如rt.jar,来自Metro或Apache CXF),以及其他可能的依赖,如XML处理库(如Woodstox或Xerces)和JAXB(Java Architecture for XML Binding)库。...
3. **JAXB(Java Architecture for XML Binding)**:用于在Java对象和XML之间进行映射,实现了XML到Java对象的自动转换,简化了数据交换。 4. **JAX-WS RI(Reference Implementation)**:Sun Microsystems(现...
在这个“java生成webservice代码用到的jar”压缩包中,可能包含了一些必要的库文件,如JAXB(Java Architecture for XML Binding)和JAX-WS相关的实现,这些库可以帮助我们生成和处理WebService客户端的代码。...
1. **数据绑定**:JAXB(Java Architecture for XML Binding)用于在Java对象和XML之间自动转换,JAX-WS支持数据绑定,使处理复杂数据类型变得更加方便。 2. **消息处理**:JAX-WS支持处理SOAP消息的不同部分,如头...
- **JAXB支持**:Java Architecture for XML Binding(JAXB)允许将Java对象自动转换为XML,反之亦然。使用JAXB,可以轻松地在REST服务中处理XML数据。 通过逐步学习和实践,开发者可以利用Jersey的强大功能构建...
- **数据绑定**:CXF支持多种数据绑定技术,如JAXB(Java Architecture for XML Binding)用于XML到Java对象的映射。 - **日志和监控**:集成日志框架如Log4j,以及使用CXF的监控功能进行性能分析和故障排查。 通过...
2. `jaxb-api.jar` - Java Architecture for XML Binding,用于对象到XML的转换。 3. `javax.xml.soap-api.jar` - 提供SOAP API。 4. `activation.jar` - JavaBeans Activation Framework,用于处理MIME类型。 5. `...
- **绑定和数据转换**:CXF通过JAXB(Java Architecture for XML Binding)实现XML到Java对象的自动转换,简化了数据交换。 2. **CXF的组件**: - **Bus**:CXF的核心是Bus组件,它管理服务的生命周期,处理消息...
3. **JAXB**:Java Architecture for XML Binding(JAXB),用于将XML数据绑定到Java对象。 ##### 示例代码 ```java import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder...
JAX-WS包括一系列API,如JAXB(Java Architecture for XML Binding)用于对象到XML的绑定,以及JAX-RS(Java API for RESTful Web Services)用于构建RESTful风格的Web服务。 教程的第一部分将引导你了解如何设置...
- **数据绑定**:CXF支持JAXB(Java Architecture for XML Binding),可以将XML文档与Java对象自动映射,简化数据交换。 - **WSDL第一**:CXF支持从WSDL(Web Service Description Language)生成服务端代码,也...