Jaxb (ing from. XSD file to Java class)

Source: Internet
Author: User

Jaxb Eclipse plug-in (. XSD file ing to Java class)

 

I. jaxb Eclipse plug-in:Https://jaxb-workshop.dev.java.net/servlets/ProjectDocumentList? Folderid = 4962 & expandfolder = 4962 & folderid = 0

2. Instructions for use of plug-ins:Https://jaxb-workshop.dev.java.net/plugins/eclipse/xjc-plugin.html

Iii. Instances

1. databaseinfo. XSD File

<? XML version = "1.0"?>
<Xs: schema xmlns: xs = "http://www.w3.org/2001/XMLSchema"
Targetnamespace = "http://www.w3school.com.cn" xmlns = "http://www.w3school.com.cn"
Elementformdefault = "qualified">

<Xs: element name = "Database">
<Xs: complextype>
<Xs: sequence>
<Xs: element name = "info" maxoccurs = "unbounded">
<Xs: complextype>
<Xs: sequence>
<Xs: element name = "ip" type = "XS: string"/>
<Xs: element name = "Port" type = "XS: string"/>
<Xs: element name = "databasename" type = "XS: string"/>
<Xs: element name = "username" type = "XS: string"/>
<Xs: element name = "password" type = "XS: string"/>
</Xs: sequence>
</Xs: complextype>
</Xs: Element>
</Xs: sequence>
</Xs: complextype>
</Xs: Element>

</Xs: schema>

2. Right-click the "databaseinfo. XSD" File


3. Next Step

4. Next Step


5. After "finish"


6. Refresh the project



7. xmloperation. Java reads and writes XML

Package com. channelsoft. xml. operation;

Import java. Io. file;

Import javax. xml. Bind. jaxbcontext;
Import javax. xml. Bind. jaxbexception;
Import javax. xml. Bind. marshaller;
Import javax. xml. Bind. unmarshaller;

Import com. channelsoft. xml. database;
Import com. channelsoft. xml. objectfactory;

Public class xmloperation
{
/**
* Read from an XML file
*/
Public void readfromxml ()
{
Jaxbcontext JC;
Try
{
JC = jaxbcontext. newinstance ("com. channelsoft. xml ");
Unmarshaller u = JC. createunmarshaller ();
Database info = (database) U. unmarshal (new file ("E: \ info. xml "));

For (INT I = 0; I <info. getinfo (). Size (); I ++)
{
System. Out. println (info. getinfo (). Get (I). getip ());
System. Out. println (info. getinfo (). Get (I). getport ());
System. Out. println (info. getinfo (). Get (I). getdatabasename ());
System. Out. println (info. getinfo (). Get (I). GetUserName ());
System. Out. println (info. getinfo (). Get (I). GetPassword ());
}

}
Catch (jaxbexception E)
{
E. printstacktrace ();
}

}

/**
* Create and write an XML file
*/
Public void savetoxml ()
{
Try
{
Objectfactory of = new objectfactory ();
Database database = of. createdatabase ();
Database. info = of. createdatabaseinfo ();

Info. setip ("10.130.14.142 ");
Info. setport ("1433 ");
Info. setdatabasename ("hrinfo ");
Info. setusername ("sa ");
Info. setpassword ("dibin ");

Database. info info1 = of. createdatabaseinfo ();

Info1.setip ("10.130.14.49 ");
Info 1.setport ("1433 ");
Info1.setdatabasename ("hrinfo ");
Info1.setusername ("sa11 ");
Info1.setpassword ("dibin11 ");

Database. getinfo (). Add (Info );
Database. getinfo (). Add (info1 );

Jaxbcontext JC = jaxbcontext. newinstance ("com. channelsoft. xml ");
Marshaller MS = JC. createmarshaller ();
Ms. setproperty ("jaxb. encoding", "gb2312 ");
Ms. setproperty ("jaxb. formatted. Output", true );
Ms. Marshal (Database, new file ("E: \ info. xml "));
}
Catch (exception E)
{

}
}

Public static void main (string [] ARGs)
{
New xmloperation (). savetoxml ();
New xmloperation (). readfromxml ();
}
}

Source code download: http://download.csdn.net/detail/a9529lty/4028420

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.