原文地址: 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
分享到:
相关推荐
THE Java™Architecture for XML Binding (JAXB) provides a fast and convenient way to bind between XML schemas and Java representations, making it easy for Java developers to incorporate XML data and ...
NULL 博文链接:https://rayoo.iteye.com/blog/1233534
在android 6.0下,应用JAXB jar包根据XML Schema解析XML文件。
JAXBDemo 的代码库。 JAXB 代表 Java Architecture for XML Binding,它允许在 XML 和 java 类之间轻松转换。
编写xsd文件,利用jaxb生成java类。
and Java Architecture for XML Binding (JAXB). The chapters on web technology have been entirely rewritten to focus on the today's most relevant topics: syndicating content with RSS and creating Web ...
此包专门针对android中的jaxb开发。 JAXB是Oracle(sun)公司的JavaBean & XML绑定框架(Java Architecture for XML Binding)
JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术。该过程中,JAXB也提供了将XML实例文档反向生成Java对象树的方法,并能将Java对象树的内容重新写到XML实例...
ycsb-hbase14-binding-0.17.0
20701:2020 Health informatics — Device interoperability — Part 20701:Point-of-care medical device communication — Service oriented medical device exchange architecture and protocol binding -...
A-PUF-FSM-Binding-Scheme-for-FPGA-IP-Protection-and-Pay-per-Device-Licensing
JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术。JAXB与xml相互转换实例。
jgoodies-binding-2_6
返利网java源码JRuby 和 JAXB:用于 XML 绑定的 Java 架构 关于 这演示了如何使用 JRuby 来控制 JAXB 类。 它的灵感来自 Jason Gilman 此示例代码侧重于构建和编组以及 XML 文档 JAXB JAXB 是源自 Java Architecture...
1. 什么是JAXB? Java Architecture for XML Binding (JAXB) 是一个业界的标准,是一项可以根据XML
真正简单的Java XML绑定(RE-JAXB)是一种将XML映射到普通的旧Java对象再映射回XML的方法,而无需使用Bean,无需外部绑定定义,无需手动映射值以及无需使用XSD文件。
JAXB (Java Architecture for XML Binding) 的主要功能是根据DTD或者XML Schema将XML Element转化为Java类。将XML文本流转化为Java Object,把XML Processing转化为对类的一些属性的访问。简单的来说,它可以让你...
Java API for JSON Binding (JSON-B).zip
RSS-XML Data-Binding to Data Control with Row Limitations。
Java EE 8 continues to improve API and programming models needed for today's applications and adds features requested by our world-wide community. This release modernizes support for many industry ...