JAXB's use (quick start) (original)

zhaozj2021-02-16  54

Sun launches JAXB (Javatm Architecture for XML Bind) has been a period of time, and the current version is 1.0 (FINAL). Let me briefly introduce it, what is JAXB and how to use it.

JAXB is a set of development interfaces and tools that automatically map XML and Java instances.

JAXB makes XML more convenient to compile an XML Schema to one or several Java Class. Provide the following functions:

From XML to Java mapping, access, update, verifying Java performance, mapping from Java to XML.

Long-term short say, start the topic, JAXB's use, we only list cases from XML to Java:

Shimo? You don't know where to download the required documents, no.

Boss! Of course, java.sun.com/xml/jaxb

First, you must get the XML file you want to convert, our DataSource as an example, the following table lists the contents of DataSource.xml:

Oracle

Oracle.jdbc.driver.Oracledriver

JDBC: Oracle: Thin: @localhost: 1521: OrCl

scott

Tiger

And, we have to have this XML DTD file (if you don't know how to get XML from DTD, or don't know how to get DTD from XML, please see this article after two days), the DataSource.dtd file is as follows:

(Name, Driver, URL, UserName, Password)

>

(Datasource)

>

(#Pcdata)

>

Element name

(#Pcdata)

>

(#Pcdata)

>

(#Pcdata)

>

(#Pcdata)

>

JAXB uses to cooperate with the XML file of a conversion rule, so we will write a DataSource.xjs file according to our needs, as shown below:

Somwhere in our document. ->

Do you think it is very complicated? Don't worry, it's ending.

The next step is to run the following command:

Java -jar% jaxb_home% / lib / jaxb-xjc-1.0-ea.jar DataSource.dtd DataSource.xjs -d Destination_Directory

You will see the Java file we want, you can open it in your design, you can open it.

OK, ready to work. This is over.

Let's take a look at how to use the Java files we have generated, don't forget to compile them before use, this is the most basic quality, I don't want me to remind it.

Below is a piece of test program, seeing to see the inflexion:

DataSources Socks = New DataSources ();

. . . .

File Socksfile = New File ("Datasource.xml");

InputStream Fin = New FileInputStream (Socksfile);

SOCKS = SOCKS.UNMARSHAL (FIN);

List socklist = socks.getdataserce ();

Ok, SockList is the object of Java we get. You can dispose at will. Let them print out and take a look:

Iterator i = sucklist.iterator (); I.hasnext ();) {

Currentsock = (DataSource) i.next ();

System.out.println (currentsock.getname ());

System.out.println (currentsock.getdriver ());

System.out.println (Currentsock.geturl ());

System.out.println (currentsock.getusername ());

System.out.println (Currentsock.getPassword ());

}

OVER!

Is this over? Is this simple?

Yes, this is this. And the operation efficiency is still very good.

转载请注明原文地址:https://www.9cbs.com/read-23501.html

New Post(0)