Reputation:
I've tried this:
@XmlSchema(xmlns=
{
@XmlNs(prefix="Person", namespaceURI="sample.url.something"),
@XmlNs(prefix="xsi", namespaceURI="http://www.w3.org/2001/XMLSchema-instance")
})
@XmlRootElement(name = "Person:sampleData")
public class Person {
private static String path = "files/test.xml";
@XmlElement()
public String Name;
@XmlElement()
public int Age;
public Person(){}
public Person(String name, int age){
this.Name = name;
this.Age = age;
}
public static String PersonToXMLString(Person person) throws JAXBException
{
JAXBContext jc = JAXBContext.newInstance(Person.class);
StringWriter sw = new StringWriter();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "somelocation.xsd");
marshaller.marshal(person, sw);
return sw.toString();
}
public static Person XMLStringToPerson() throws JAXBException
{
JAXBContext jaxbContext = JAXBContext.newInstance(Person.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Person Person = (Person) unmarshaller.unmarshal(new File(path));
return Person;
}
public static void WriteXMLStringFile(String xml) throws IOException
{
File file = new File(path);
try (FileOutputStream fop = new FileOutputStream(file)) {
if (!file.exists()) {
file.createNewFile();
}
byte[] contentInBytes = xml.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String ReadXmlStringFromFile() throws IOException
{
BufferedReader br = new BufferedReader(new FileReader(new File(path)));
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line.trim());
}
return sb.toString();
}
public static void main(String[] args) throws JAXBException, IOException
{
Person user = new Person("User",23);
String xml = user.PersonToXMLString(user);
System.out.println(xml);
user.WriteXMLStringFile(xml);
xml = user.ReadXmlStringFromFile();
//used to unmarshall xml to Person object
Person person = user.XMLStringToPerson();
System.out.println(person.Name);
}
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Person:sampleData xmlns:Person="sample.url.something" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Name>User</Name>
<Age>23</Age>
</Person:sampleData>
If i do something like this i get exception on unmarshalling:
Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"sample.url.something", local:"sampleData"). Expected elements are <{}Person:sampleData>`
FYI: I can not modify the XML.
Any help gratefully received!
Upvotes: 2
Views: 2868
Reputation: 148977
You can do the following:
package-info.java
Your @XmlSchema
annotation should look something like the following on your package-info
class. Since elementFormDefault
is specified as UNQUALIFIED
the namespace will only be applied to global elements (in JAXB those corresponding to the @XmlRootElement
). Note a JAXB impl is not required to use the prefix included in the @XmlSchema
when marshalling XML.
@XmlSchema(
elementFormDefault=XmlNsForm.UNQUALIFIED,
namespace="sample.url.something",
xmlns={
@XmlNs(prefix="Person", namespaceURI="sample.url.something")
}
)
package com.example;
import javax.xml.bind.annotation.*;
Person
The @XmlRootElement
annotation should not include the prefix.
package com.example;
import javax.xml.bind.annotation.*;
@XmlRootElement(name="sampleData")
public class Person {
For More Information
You can read more about controlling namespace prefixes in JAXB on my blog:
Upvotes: 1