Tool category
public class Ben2xmlutil {
/**
* Convert objects directly to XML output of type string
*
* @param obj
* @return
*/
public static String converttoxml (Object obj) {
Create an output stream
StringWriter SW = new StringWriter ();
try {
Using the transform class that comes with the JDK
Jaxbcontext context = Jaxbcontext.newinstance (Obj.getclass ());
Marshaller Marshaller = Context.createmarshaller ();
Format the XML output
Marshaller.setproperty (Marshaller.jaxb_formatted_output, boolean.true);
Convert an object to an XML in the form of an output stream
Marshaller.marshal (obj, SW);
} catch (Jaxbexception e) {
E.printstacktrace ();
}
return sw.tostring ();
}
Custom namespace Prefixes
public static class Preferredmapper extends Namespaceprefixmapper {
@Override
public string Getpreferredprefix (string NamespaceURI, String suggestion, Boolean Requireprefix) {
return "OS2";
}
}
public static void Main (string[] args) throws Jaxbexception, FileNotFoundException {
Ceb622message customer = new Ceb622message ();
Book books = new book ();
Customer.getinventoryreturn (). Add (books);
To convert a Java object to an XML string
String xmlstr = converttoxml (customer);
System.out.println (XMLSTR);
The custom name space and later able inherit the Namespaceprefixmapper
Jaxbcontext context = Jaxbcontext.newinstance (Customer.getclass ());
Marshaller m = Context.createmarshaller ();
Namespaceprefixmapper mapper = new Preferredmapper ();
M.setproperty ("Com.sun.xml.bind.namespacePrefixMapper", mapper);
M.setproperty (Marshaller.jaxb_formatted_output, true);
M.marshal (Customer, System.out);
}
}
Entity
Package cst.goodsList.entity;
Import java.util.ArrayList;
Import java.util.List;
Import javax.xml.bind.annotation.*;
@XmlRootElement (name = "Ceb622message")
@XmlAccessorType (Xmlaccesstype.field)
@XmlType (name = "Name")
public class Ceb622message {
Private list<book> Inventoryreturn;
@XmlAttribute
Private String GUID = "123";
Public String GetGuid () {
return GUID;
}
public void SetGuid (String guid) {
This.guid = GUID;
}
Public Ceb622message () {
Inventoryreturn = new arraylist<> ();
}
Public list<book> Getinventoryreturn () {
return inventoryreturn;
}
public void Setinventoryreturn (list<book> inventoryreturn) {
Inventoryreturn = Inventoryreturn;
}
}
Entity two//put in real fields
Package cst.goodsList.entity;
public class Book {
Private String GUID = "AA";
Private String Customscode = "BB";
Private String Ebpcode = "CC";
Private String Ebccode = "DD";
Private String Agentcode = "EE";
Private String Copno = "FF";
Private String Preno = "GG";
Private String returnstatus= "HH";
Private String returntime= "JJ";
Private String returninfo= "QQ";
Public String GetGuid () {
return GUID;
}
public void SetGuid (String guid) {
This.guid = GUID;
}
Public String Getcustomscode () {
return customscode;
}
public void Setcustomscode (String customscode) {
This.customscode = Customscode;
}
Public String Getebpcode () {
return ebpcode;
}
public void Setebpcode (String ebpcode) {
This.ebpcode = Ebpcode;
}
Public String Getebccode () {
return ebccode;
}
public void Setebccode (String ebccode) {
This.ebccode = Ebccode;
}
Public String Getagentcode () {
return agentcode;
}
public void Setagentcode (String agentcode) {
This.agentcode = Agentcode;
}
Public String Getcopno () {
return copno;
}
public void Setcopno (String copno) {
This.copno = Copno;
}
Public String Getpreno () {
return Preno;
}
public void Setpreno (String preno) {
This.preno = Preno;
}
Public String Getreturnstatus () {
return returnstatus;
}
public void Setreturnstatus (String returnstatus) {
This.returnstatus = ReturnStatus;
}
Public String Getreturntime () {
return returntime;
}
public void Setreturntime (String returntime) {
This.returntime = Returntime;
}
Public String Getreturninfo () {
return returninfo;
}
public void Setreturninfo (String returninfo) {
This.returninfo = Returninfo;
}
}
Anti-sequence session effect
<?xml version= "1.0" encoding= "UTF-8"?>
<os2:ceb622message os2:xmlns= "Com.sun.xml.bind.namespacePrefixMapper" guid= "123" >
<InventoryReturn>
<agentCode>EE</agentCode>
<copNo>FF</copNo>
<customsCode>BB</customsCode>
<ebcCode>DD</ebcCode>
<ebpCode>CC</ebpCode>
<guid>AA</guid>
<preNo>GG</preNo>
<returnInfo>QQ</returnInfo>
<returnStatus>HH</returnStatus>
<returnTime>JJ</returnTime>
</InventoryReturn>
<os2:/CEB622Message>
------------------------------------------------------code is complete---------------------------------------------------------
Jaxb-java object to XML string