Please tell me how you can use Marshaller to form an xml - structure like:

<p:DCTRequest xmlns:p="www.**.com" xmlns:p1="www.**.com/datatypes" xmlns:p2="www.**.com/DCTRequestdatatypes" xmlns:xsi="w3.org/2001/XMLSchema-instance"> </<p:DCTRequest > 

Here is the code for my class:

 import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement (name = "DCTRequest") @XmlAccessorType(XmlAccessType.FIELD) public class DCTRequestType { @XmlElement(name = "GetQuote") protected GetQuoteType GetQuote ; public void setGetQuote (GetQuoteType quote){ this.GetQuote = quote; } public GetQuoteType getGetQuote() { return this.GetQuote; } } 

1 answer 1

If you just need to specify a namespace, then there are two options.

The first is to indicate in the annotation:

 @XmlRootElement(name = "DCTRequest", namespace = "http://***.com") 

The second is to create a package-info.java file in the same package with the following contents:

 @javax.xml.bind.annotation.XmlSchema(namespace = "http://***.com", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) package your.package.name; 

In both cases, the output will be:

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <DCTRequest xmlns="http://***.com"> ... </DCTRequest> 

If it is necessary that the prefix name be "p", then we modify package-info.java in the second version:

 @XmlSchema(namespace = "http://***.com", xmlns = @XmlNs(prefix = "p", namespaceURI = "http://***.com"), elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) package your.package.name; import javax.xml.bind.annotation.XmlNs; import javax.xml.bind.annotation.XmlSchema; 

the output will be:

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <p:DCTRequest xmlns:p="http://***.com"> ... </p:DCTRequest> 

If you need several prefixes, you can write this:

 @XmlSchema(namespace = "http://***.com", xmlns = { @XmlNs(prefix = "p", namespaceURI = "http://***.com"), @XmlNs(prefix = "p1", namespaceURI = "www.**.com/datatypes"), @XmlNs(prefix = "p2", namespaceURI = "www.**.com/DCTRequestdatatypes") }, elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) package test.aaa; import javax.xml.bind.annotation.XmlNs; import javax.xml.bind.annotation.XmlSchema; 

and just the elements indicate the namespace, and the prefixes themselves are substituted.

  • Good day. I tried to do as you said: - Renat Ahmetshin
  • I created the package-ino.java file in the project, but for some reason Lotus does not understand the structure that I want to get from it. <ns2: DCTRequest xmlns: ns2 = " dhl.com "> this is how the tag is formed. Tell me, what could be wrong? - Renat Ahmetshin
  • @RenatAhmetshin and what does the Lotus and what you get from it? It's about how to generate xml in java. If you are in the same package where DCTRequestType.java create package-info.java as I described and specify the namespace of XmlRootElement and XmlElement, get what I wrote. If Lotus does not give something or does not understand something, then this is already a separate question. - Russtam