I am using JAXB2 marshaller to create an xml file based on the generated classes from the xml schema. I got two questions regarding the namespace:
1. In the root element "ROOT", I need to add attributes xsi:schemaLoaction, xmlns, and xmlns:xsi as below
<?xml version="1.0" encoding="UTF-8" ?>
But from the generated class ROOT, I am not able to find the setSchemaLocation() or SetXmlns(), How do I set these values to ROOT element?
2. There is another element class that has setCity() method, and this method takes JAXBElement<String> as argument. I created the JAXBElement<String> as below:
JAXBElement<String> city = new JAXBElement<String>(new QName("city"), String.class, null, "New York"); //cannot get rid of namespace
And the result xml is
<city xmlns:ns2="http://www.mycom.com/XMLSchema/myxsd" xmlns="">New York</city>
I need <city>New York</city>, and how can I get rid of the namespace?
I don't know the answer to (1). But the answer to (2) is that you don't have to do anything. In both examples you have a <city> element which is not in any namespace. It's true that one of them also has an extra namespace declaration, but that doesn't apply to anything in the element. Your two examples are functionally identical.
If the reason for setting the schema location is that you want to perform validation against the XML schema when marshalling or unmarshalling, then setting the schema should be done programmatically using the Marshaller or Unmarshaller classes.