I have an application that can save a file in various formats (all of them are xml). Therefore, I have to solve the problem with the definition in the format file. So I see 2 solutions
- different formats have different schemes, so I can define them with them. I set the circuits in such a way that I get from here
marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, "bla-bla.xsd");
so I can get it with unmarshaller.getProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION)
but he throws
javax.xml.bind.PropertyException: jaxb.noNamespaceSchemaLocation
and getSchema() return null So, how can I get the location of the schema?
- Specifying different adapters for different beans using the
setAdapter(Class<A> type, A adapter) method
Which method is preferable? If at first, how can I get the location label of the circuit?
sample code suppose bean
@XmlRootElement public class Foo{ String bar; public String getBar() {return bar; } public void setBar(String bar) {this.bar = bar;} }
and the code that generates the circuit then stores the instances of Foo and loads.
public class Test { final static String schemaLoc = "fooschema.xsd"; public static void write(File file, Foo foo, Schema schema) throws Throwable { XMLEventWriter xsw = null; try{ JAXBContext context = JAXBContext.newInstance(Foo.class); XMLOutputFactory xof = XMLOutputFactory.newInstance(); OutputStream out = new FileOutputStream(file); xsw = xof.createXMLEventWriter(out); Marshaller m = context.createMarshaller(); m.setSchema(schema);
which generates
<xs:element name="foo" type="foo"/> <xs:complexType name="foo"> <xs:sequence> <xs:element name="bar" type="xs:string" minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:schema>
our temporary file
<?xml version="1.0"?> <foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="fooschema.xsd"> <bar>test bar</bar></foo>
as we see there
XSI: noNamespaceSchemaLocation = "fooschema.xsd"
How can I get this text using JAXB?
source share