Reputation: 533
I saw that this has been asked other time and I've reviewed the answer but I should admit that I'm still not able to get the desired output :( Now what I would like to have is:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tokenregistrationrequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="TOKEN">
<purchase>true</purchase>
</tokenregistrationrequest>
My class definition look like the following:
@XmlRootElement() //THIS HAS BEEN ADDED MANUALLY
public class Tokenregistrationrequest {
protected boolean purchase;
}
and I've modified the package-info like the following:
@javax.xml.bind.annotation.XmlSchema(xmlns = {
@javax.xml.bind.annotation.XmlNs(prefix = "xsi", namespaceURI = "http://www.w3.org/2001/XMLSchema-instance"),
@javax.xml.bind.annotation.XmlNs(prefix = "xs", namespaceURI = "http://www.w3.org/2001/XMLSchema"),
@javax.xml.bind.annotation.XmlNs(prefix = "", namespaceURI = "TOKEN")
})
When I'm running the code what I got is just...
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tokenregistrationrequest>
<purchase>true</purchase>
</tokenregistrationrequest>
I'm pretty sure I'm missing something basic here... Any help would greatly appreciated
EDIT: while doing some more test I've seen that when compiling my test class JAXB artifact are compiled but NOT package-info.java. Is this because the usage of this is optional?
Cheers
Upvotes: 1
Views: 3434
Reputation: 145
It's been a while since the question asked, but i just found myself in a similar situation. It's a bit disturbing but it's possible with this way if you really insist of it.
You just can add these properties manually as an attribute to your entity class.
@XmlAttribute(name = "xmlns")
private String token = "TOKEN";
@XmlAttribute(name = "xmlns:xsd")
private String schema = "http://www.w3.org/2001/XMLSchema";
@XmlAttribute(name = "xmlns:xsi")
private String schemaInstance = "http://www.w3.org/2001/XMLSchema-instance";
Then you should get this as output :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tokenregistrationrequest xmlns="TOKEN"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<purchase>true</purchase>
</tokenregistrationrequest>
Upvotes: 1
Reputation: 41
The Annotation XmlNs on package Level binds a prefix to a Namespace. However, if the Namespace is not used, like in your case, the Namespace nodes will not be added to the root-element.
You propably should put the Tokenregistrationrequest in the TOKEN Namespace:
@XmlRootElement(namespace = "TOKEN") //THIS HAS BEEN ADDED MANUALLY
public class Tokenregistrationrequest {
protected boolean purchase;
This will result in
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tokenregistrationrequest xmlns="TOKEN">
<purchase>true</purchase>
</tokenregistrationrequest>
The rest of the prefix declarations won't appear, as there are no nodes from those namespaces in the resulting document. If there were any, the prefix declarations should also be serialized.
Upvotes: 0