From a common JAXB model the xml generated can be of the form
<ipi-list><ipi>1001</ipi><ipi>1002</ipi></ipi-list>
because in json we have arrays we dont need both elements, so by using MOXy's oxml extensions I can flatten the output to give
"ipi" : [ "1001", "1002" ],
but because ipi now refers to an array of things I would like it to be called ipis not ipi
"ipis" : [ "1001", "1002" ],
Is there a way to get MOXy to rename an element ?
You could use EclipseLink JAXB (MOXy)'s external mapping document to tweak the mapping for either the XML or JSON representation.
IPIList
Below is a domain class with JAXB annotations that matches the XML representation from your question:
package forum11449219;
import java.util.*;
import javax.xml.bind.annotation.*;
#XmlRootElement(name="ipi-list")
public class IPIList {
private List<String> list = new ArrayList<String>();
#XmlElement(name="ipi")
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
}
oxm.xml
We can use MOXy's external mapping document to modify how the list property is mapped to JSON.
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="forum11449219">
<java-types>
<java-type name="IPIList">
<java-attributes>
<xml-element java-attribute="list" name="ipis"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
jaxb.properties
To specify MOXy as your JAXB provider you need to include a file called jaxb.properties in the same package as your domain model with the following entry (see ):
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Demo
The following demo code shows how to reference the external mapping document when creating a JAXBContext.
package forum11449219;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
public class Demo {
public static void main(String[] args) throws Exception {
IPIList ipiList = new IPIList();
ipiList.getList().add("1001");
ipiList.getList().add("1002");
// XML
JAXBContext jc = JAXBContext.newInstance(IPIList.class);
Marshaller xmkMarshaller = jc.createMarshaller();
xmkMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
xmkMarshaller.marshal(ipiList, System.out);
// JSON
Map<String, Object> properties = new HashMap<String, Object>(3);
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum11449219/oxm.xml");
properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
JAXBContext jsonJC = JAXBContext.newInstance(new Class[] {IPIList.class}, properties);
Marshaller jsonMarshaller = jsonJC.createMarshaller();
jsonMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jsonMarshaller.marshal(ipiList, System.out);
}
}
Output
Here is the output from running the demo code:
<?xml version="1.0" encoding="UTF-8"?>
<ipi-list>
<ipi>1001</ipi>
<ipi>1002</ipi>
</ipi-list>
{
"ipis" : [ "1001", "1002" ]
}
For More Information
http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html
http://blog.bdoughan.com/2012/04/extending-jaxb-representing-metadata-as.html
http://blog.bdoughan.com/2011/09/mapping-objects-to-multiple-xml-schemas.html
Related
Please help!
I am trying to unmarshall the below XML STRING to a java class. The requirement is only to grab some elements and not all:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet type='text/xsl' href='http://myService/rs/../xsl/searchRetrieveResponse.xsl'?>
<searchRetrieveResponse
xmlns="http://www.loc.gov/zing/srw/"
xmlns:srw5="info:srw/extension/5/restrictorSummary">
<version>1.2</version>
<numberOfRecords>1</numberOfRecords>
<records>
<record>
<recordSchema>info:srw/schema/1/CDFXML</recordSchema>
<recordPacking>xml</recordPacking>
<recordData>
<institution active="true" test="true" training="false"
xmlns="info:rfa/rfaRegistry/xmlSchemas/institution"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="info:rfa/rfaRegistry/xmlSchemas/institution http://worldcat.org/registry/xsd/collections/Institutions/institution.xsd">
<identifier>info:rfa/Institutions/113500</identifier>
<versionID>2016-02-17T20:01:22.355Z</versionID>
<nameLocation
xmlns="info:rfa/rfaRegistry/xmlSchemas/institutions/nameLocation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="info:rfa/rfaRegistry/xmlSchemas/institutions/nameLocation http://worldcat.org/registry/xsd/collections/Institutions/nameLocation.xsd">
<lastUpdated>2015-09-27</lastUpdated>
<lastUpdatedTime>03:06:43</lastUpdatedTime>
<first>First Name</first>
</nameLocation>
<identifiers
xmlns="info:rfa/rfaRegistry/xmlSchemas/institutions/identifiers"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="info:rfa/rfaRegistry/xmlSchemas/institutions/identifiers http://worldcat.org/registry/xsd/collections/Institutions/identifiers.xsd">
<lastUpdated>2016-02-17</lastUpdated>
<lastUpdatedTime>15:01:22</lastUpdatedTime>
<age>23</age>
<age>55</age>
</identifiers>
<opac available="true" intranetOnly="false"
xmlns="info:rfa/rfaRegistry/xmlSchemas/institutions/opac"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="info:rfa/rfaRegistry/xmlSchemas/institutions/opac http://worldcat.org/registry/xsd/collections/Institutions/opac.xsd">
<lastUpdated>2009-12-03</lastUpdated>
<lastUpdatedTime>17:43:52</lastUpdatedTime>
<url1>facebook</url1>
<url2>google</url2>
<prefix/>
</opac>
</institution>
</recordData>
<recordPosition>1</recordPosition>
</record>
</records>
</searchRetrieveResponse>
From this file I am only looking to map these tags in my java class below
<first> <age> <age> <url1> <url2>
Java class:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name = "searchRetrieveResponse")
public class MyInfo {
#XmlElement(name = "first")
private String first;
#XmlElement(name = "age")
private List<String> age;
#XmlElement(name = "url1")
private String url1;
#XmlElement(name = "url2")
private String url2;
}
Unmarshalling implementation:
public static void main(String[] args) {
JAXBContext jaxbContext;
try {
jaxbContext = JAXBContext.newInstance(MyInfo.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
MyInfo myInfo = (MyInfo) jaxbUnmarshaller.unmarshal(
new StringReader( * * MY_XML_STRING **));
catch(JAXBException ex){
log.error("Error - ", ex);
}
}
Error:
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.loc.gov/zing/srw/", local:"searchRetrieveResponse"). Expected elements are <{}searchRetrieveResponse>
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:744)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:262)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1149)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:574)...
In My gradle dependencies:
jaxbApi : "javax.xml.bind:jaxb-api:2.4.0-b180830.0359",
jaxbImpl : "com.sun.xml.bind:jaxb-impl:2.4.0-b180830.0438",
jaxbCore : "org.glassfish.jaxb:jaxb-core:2.3.0.1",
And java 11 is the complier.
Add: , namespace = "http://www.loc.gov/zing/srw/"
to your #XmlRootElement:
#XmlRootElement(name = "searchRetrieveResponse", namespace = "http://www.loc.gov/zing/srw/")
...this is what the error message complains about: A mismatch in the namespace!
unexpected element (uri:"http://www.loc.gov/zing/srw/", local:"searchRetrieveResponse").
Expected elements are <{}searchRetrieveResponse>
The {} means empty ("") namespace, in this case.
If namespace omit, it defaults to "##default"...
If the value is "##default", then the XML namespace name is derived from the package of the class ( XmlSchema ). If the package is unnamed, then the XML namespace is the default empty namespace.
EDIT:
In your scenario, I would generate the classes with (built-in) xjc (command line tool ...in ${java.home}/bin)
xjc -d generated <schema>
...where gnerated will be the output folder for the classes (.java files) and <schema> can be a file URL or (structured)directory (with your (readonly) xsd(s)).
Once successfully generated, copy the content of generated into your "main source folder", check it in(, use it) and only change it, when xsd changes.
I am using following code:
JAXBContext jaxbContext = (JAXBContext) JAXBContext.newInstance(AFDType.class);
AFDType AFDType = new AFDType();
DeviceInfoType devInfoType = new DeviceInfoType();
DeviceDetailInfoType devDetailInfoType = new DeviceDetailInfoType();
devDetailInfoType.setEsnMeidDec("esn1234");
devInfoType.setBrandCode("brand123");
devInfoType.setSkuType("sku12345");
devInfoType.setDeviceDetailInfo(devDetailInfoType);
AFDType.setDeviceInfo(devInfoType);
JAXBMarshaller marshaller = jaxbContext.createMarshaller();
StringWriter sw = new StringWriter();
marshaller.setProperty(JAXBMarshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(AFDType, sw);
This generates an xml of following format:
<?xml version="1.0" encoding="UTF-8"?>
<ns0:deviceInfo>
<ns0:deviceDetailInfo>
<esnMeidDec>esn1234</esnMeidDec>
</ns0:deviceDetailInfo>
<ns0:brandCode>brand123</ns0:brandCode>
<ns0:skuType>sku12345</ns0:skuType>
</ns0:deviceInfo>
However i require following xml:
<ns0:addFD xmlns:ns0="http://ee.abc.com/interfaces/afd/v1/afd.xsd">
<ns0:deviceInfo>
<ns0:deviceDetailInfo>
<ns0:esnMeidHex>36748298746378</ns0:esnMeidHex>
</ns0:deviceDetailInfo>
<ns0:brandCode>SPR</ns0:brandCode>
<ns0:skuType>BYO</ns0:skuType>
</ns0:deviceInfo>
</ns0:addFD>
Can someone please help me how to obtain the 2nd xml.
Add package-info.java into interfaces.afd root folder with the following content
#XmlSchema(
elementFormDefault=XmlNsForm.QUALIFIED,
namespace="http://ee.abc.com/interfaces/afd/v1",
xmlns={#XmlNs(prefix="ns0",
namespaceURI="http://ee.abc.com/interfaces/afd/v1")}
)
package interfaces.afd;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
Read more about it here.
The enclosing root element <ns0:addFd></ns0:addFd> can be achieved by adding #XmlRootElement(name = "addFd") tag above the AddFd.class like below :
#XmlRootElement(name = "addFd")
public class AddFd {
..
}
I want to generate this below mentioned xml file from my multiple action classes in struts2.
I perform multiple below mentioned operations from my Struts 2 action classes and adding them in single existing xml file.
1) Adding news in xml file
2) Adding footer info in xml file
3) Adding agency info in xml file.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<agencylink>
<agency>
<title>Ministry of Games</title>
<url>http://www.abc.gov.</url>
</agency>
<agencyNews>
<date>22/Apr/2014</date>
<news>How do I clear my web browsers cache, cookies, and history?</news>
</agencyNews>
<headerLogo>
<img>images/header.jpg</img>
</headerLogo>
<footerText>
<ftxt>All rights reserved.</ftxt>
</footerText>
<aLoginLogo>
<img>images/logo1.jpg</img>
</aLoginLogo>
<aHeaderLogo>
<img>images/logo2.jpg</img>
</aHeaderLogo>
</agencylink>
I am using JaxB.
When i am performing this different-different actions, my xml file is appended with that info but issue is tag name is same when it stored in xml file.
Tag name must be looks like above mentioned xml file.
When i update news information all tag name is changed as <agencyNews>.
So please any one help me to get this xml format through JaxB.
Its my Java Code below
package com.ocr.nsw.xml;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;
#XmlRootElement(name = "agencylink")
#XmlAccessorType(XmlAccessType.FIELD)
#XmlSeeAlso({ Agency.class, AgencyNews.class, HeaderLogo.class,
FooterText.class, ALoginLogo.class, AHeaderLogo.class })
public class AgencyLink {
#XmlElement(name = "agencyNews")
private List<AgencyNews> adate;
#XmlElement(name = "agencyNews")
private List<AgencyNews> anews;
#XmlElement(name = "agency")
private List<Agency> atitle;
#XmlElement(name = "agency")
private List<Agency> aurl;
#XmlElement(name = "footerText")
private List<FooterText> aftxt;
#XmlElement(name = "headerLogo")
private List<HeaderLogo> headerLogoImg;
#XmlElement(name = "aLoginLogo")
private List<ALoginLogo> loginLogoImg;
#XmlElement(name = "aHeaderLogo")
private List<AHeaderLogo> aHeaderLogoImg;
}
_____
public static void xmlWrite(List list, String filePath) {
AgencyLink agencyLink = new AgencyLink();
agencyLink.setAurl(null);
agencyLink.setAnews(list);
agencyLink.setAtitle(null);
agencyLink.setAftxt(null);
agencyLink.setHeaderLogoImg(null);
agencyLink.setLoginLogoImg(null);
agencyLink.setaHeaderLogoImg(null);
if (marshalling(filePath, agencyLink)) {
System.out.println("Success");
}
}
------
#SuppressWarnings("rawtypes")
public static Object unMarshalling(String filePath, Class clazz) {
try {
File file = new File(filePath);
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Object obj = clazz.cast(jaxbUnmarshaller.unmarshal(file));
return obj;
} catch (JAXBException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
From this code i got this below mentioned XML File
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<agencylink>
<agencyNews xsi:type="agency" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<title>Ministry of Health</title>
<url>http://www.abc.gov.bn</url>
</agencyNews>
<agencyNews>
<date>22/Apr/2014</date>
<news>How do I clear my web browsers cache, cookies, and history?</news>
</agencyNews>
<agencyNews>
<date>20/10/2013</date>
<news>10</news>
</agencyNews>
<agencyNews xsi:type="headerLogo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<img>images/header.jpg</img>
</agencyNews>
<agencyNews xsi:type="footerText" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ftxt>All rights reserved</ftxt>
</agencyNews>
<agencyNews xsi:type="aLoginLogo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<img>images/logo1.jpg</img>
</agencyNews>
<agencyNews xsi:type="aHeaderLogo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<img>images/logo2.jpg</img>
</agencyNews>
</agencylink>
I have been trying to generate XML file from lists of object of type Customer class.
Which i have done successfully.But the structure of my XML file is nested and i want to flatten it out.
Following is my XML file content:
<Customers>
<customer>
<accessRole>
<name>Customer Center</name>
<internalId>14</internalId>
</accessRole>
<aging>0.0</aging>
<category>
<name>Individual</name>
<internalId>2</internalId>
</category>
<companyName>Wolfe Electronics</companyName>
<consolAging>0.0</consolAging>
<consolBalance>0.0</consolBalance>
<consolDepositBalance>0.0</consolDepositBalance>
<consolOverdueBalance>0.0</consolOverdueBalance>
<consolUnbilledOrders>2705.23</consolUnbilledOrders>
<creditHoldOverride/>
<dateCreated>2011-06-22T12:30:00+05:30</dateCreated>
<defaultAddress>US</defaultAddress>
<displaySymbol>$</displaySymbol>
<email>suresh#ldbsystems.com</email>
<emailPreference/>
<emailTransactions>false</emailTransactions>
<entityId>A Wolfe</entityId>
<entityStatus>
<name>CUSTOMER-Closed Won</name>
<internalId>13</internalId>
</entityStatus>
<externalId>entity-5</externalId>
<faxTransactions>false</faxTransactions>
<firstName>A</firstName>
<firstVisit>2012-02-25T05:43:18+05:30</firstVisit>
<giveAccess>true</giveAccess>
<globalSubscriptionStatus/>
<internalId>-5</internalId>
<isBudgetApproved>false</isBudgetApproved>
<isInactive>false</isInactive>
<isPerson>true</isPerson>
<lastModifiedDate>2012-12-19T07:12:03+05:30</lastModifiedDate>
<lastName>Wolfe</lastName>
<lastPageVisited>login-register</lastPageVisited>
<lastVisit>2013-02-23T06:10:44+05:30</lastVisit>
<overrideCurrencyFormat>false</overrideCurrencyFormat>
<phone>650-555-9788</phone>
<priceLevel>
<name>Employee Price</name>
<internalId>3</internalId>
</priceLevel>
<printTransactions>false</printTransactions>
<receivablesAccount>
<name>Use System Preference</name>
<internalId>-10</internalId>
</receivablesAccount>
<salesRep>
<name>Clark Koozer</name>
<internalId>23</internalId>
</salesRep>
<shipComplete>false</shipComplete>
<stage/>
<symbolPlacement/>
<taxItem>
<name>CA-SAN MATEO</name>
<internalId>-112</internalId>
</taxItem>
<taxable>false</taxable>
<unbilledOrders>2705.23</unbilledOrders>
<visits>150</visits>
<webLead>No</webLead>
</customer>
<customer>
<accessRole>
<name>Customer Center</name>
<internalId>14</internalId>
</accessRole>
<aging>0.0</aging>
<category>
<name>Corporate</name>
<internalId>1</internalId>
</category>
<consolAging>0.0</consolAging>
<consolBalance>0.0</consolBalance>
<consolDepositBalance>0.0</consolDepositBalance>
<consolOverdueBalance>0.0</consolOverdueBalance>
<consolUnbilledOrders>76.8</consolUnbilledOrders>
<creditHoldOverride/>
<customFieldList>
<customField/>
</customFieldList>
<dateCreated>2011-06-26T12:30:00+05:30</dateCreated>
<defaultAddress>Anderson Boughton Inc.<br>1488 Main<br>Apt 113<br>Seattle WA 98106<br>US</defaultAddress>
<displaySymbol>$</displaySymbol>
<email>boughton751#cscatering.com</email>
<emailPreference/>
<emailTransactions>false</emailTransactions>
<entityId>Anderson Boughton Inc.</entityId>
<entityStatus>
<name>CUSTOMER-Closed Won</name>
<internalId>13</internalId>
</entityStatus>
<faxTransactions>false</faxTransactions>
<firstVisit>2012-07-12T01:30:49+05:30</firstVisit>
<giveAccess>false</giveAccess>
<globalSubscriptionStatus/>
<internalId>75</internalId>
<isBudgetApproved>false</isBudgetApproved>
<isInactive>false</isInactive>
<isPerson>false</isPerson>
<lastModifiedDate>2012-12-19T11:50:14+05:30</lastModifiedDate>
<lastPageVisited>HP xw4100</lastPageVisited>
<lastVisit>2012-07-12T01:30:49+05:30</lastVisit>
<leadSource>
<name>Partner Referral</name>
<internalId>99993</internalId>
</leadSource>
<overrideCurrencyFormat>false</overrideCurrencyFormat>
<partner>
<name>Online electronics</name>
<internalId>171</internalId>
</partner>
<phone>206-555-1302</phone>
<priceLevel>
<name>Base Price</name>
<internalId>1</internalId>
</priceLevel>
<printTransactions>false</printTransactions>
<receivablesAccount>
<name>Use System Preference</name>
<internalId>-10</internalId>
</receivablesAccount>
<salesRep>
<name>A Wolfe</name>
<internalId>-5</internalId>
</salesRep>
<shipComplete>false</shipComplete>
<stage/>
<symbolPlacement/>
<taxable>false</taxable>
<unbilledOrders>76.8</unbilledOrders>
<webLead>No</webLead>
</customer>
</Customers>
So i have nested structure for <accessRole> which i want to flatten out like
<accessRole-name>and i don't want to display element like <internalId>.
Please find a code snippet of my model:
try {
FileWriter fileWriter = new FileWriter ("D:\\adapter-framework\\Customer_XML.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(extractedRecordsArray[0].getClass());
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
fileWriter.append("<Customers>");
fileWriter.append('\n');
for(int i = 0; i < extractedRecordsArray.length - 761 ; i++)
{
jaxbMarshaller.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
//jaxbMarshaller.marshal(extractedRecordsArray[i],fileWriter);
jaxbMarshaller.marshal(extractedRecordsArray[i],System.out);
fileWriter.append('\n');
AdapterLogger.debug(this.getClass().getName(), "Extracted record number : " + i);
}
fileWriter.append("</Customers>");
fileWriter.close();
} catch (JAXBException e) {
e.printStackTrace();
}
Please help me out if someone knows about it.
Thanks.
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
You can use the #XmlPath extension in MOXy to flatten the XML structure.
Customer
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
#XmlPath(".")
private AccessRole accessRole;
}
AccessRole
import javax.xml.bind.annotation.*;
#XmlAccessorType(XmlAccessType.FIELD)
public class AccessRole {
#XmlElement(name="accessRoleName")
private String name;
}
For More Information
http://blog.bdoughan.com/2010/07/xpath-based-mapping.html
I am trying to serialize an XML from a class generated via JAXB.
The class:
package ws.avail;
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"pos",
"uniqueID",
"availStatusMessages"
})
#XmlRootElement(name = "OTA_HotelAvailNotifRQ")
public class OTAHotelAvailNotifRQ {
...
}
File "package-info.java":
#javax.xml.bind.annotation.XmlSchema(namespace = "http://www.opentravel.org/OTA/2003/05", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package ws.avail;
The marshaller (simplified without error handling):
context = JAXBContext.newInstance(rootElement.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(rootElement, stringWriter);
return stringWriter.toString();
This code generates:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<OTA_HotelAvailNotifRQ Version="1.000" Target="TEST">
....
I expect something like this:
<?xml version="1.0" encoding="UTF-8"?>
<OTA_HotelAvailNotifRQ xmlns="http://www.opentravel.org/OTA/2003/05" Version="1.000" Target="TEST">
....
I have a similar code and works correctly, but I am unable to see why this code doesn't display the namespace. Any clue? Thanks!
YOUR MAPPINGS ARE CORRECT
I tried out your model and it works for me:
Demo
package ws.avail;
import java.io.StringWriter;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
OTAHotelAvailNotifRQ rootElement = new OTAHotelAvailNotifRQ();
StringWriter stringWriter = new StringWriter();
JAXBContext context = JAXBContext.newInstance(rootElement.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(rootElement, stringWriter);
System.out.println(stringWriter.toString());
}
}
Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><OTA_HotelAvailNotifRQ xmlns="http://www.opentravel.org/OTA/2003/05"/>
SOLUTION
Usually when this problem occurs the package-info class was not compiled or not packaged with the application.
FOR MORE INFORMATION
http://blog.bdoughan.com/2010/08/jaxb-namespaces.html