पर नामस्थान को अनदेखा करता है मैं जैक्सबी 2 और वसंत का उपयोग करता हूं। मैं अपने कुछ ग्राहकों द्वारा भेजे गए कुछ एक्सएमएल को उतारने की कोशिश कर रहा हूं।जैक्सब unmarshalling
<foo xmlns="com.acme">
<bar>[...]</bar>
<foo>
है कि इस तरह एक POJO के लिए बाध्य है:
@XmlType(name = "", propOrder = {"bar"})
@XmlRootElement(name = "Foo")
public class Foo {
@XmlElement(name = "Bar")
private String bar;
[...]
}
मुझे लगता है कि पिछले खोज की
अब तक, मैं केवल एक ग्राहक जो इस तरह की कुछ xml भेजा संभाल करने के लिए किया था डेवलपर ने इसे काम करने के लिए unmarshaller में नेमस्पेस हार्डकोड किया।
अब, दूसरा ग्राहक एक ही एक्सएमएल भेजता है लेकिन नामस्थान बदलता है!
<foo xmlns="com.xyz">
<bar>[...]</bar>
<foo>
जाहिर है, unmarshaller unmarshall क्योंकि यह {com.xyz}foo
के बजाय कुछ {com.acme}foo
उम्मीद विफल रहता है। अनजाने में, ग्राहक को एक्सएमएल बदलने के लिए कहा एक विकल्प नहीं है।
मैं क्या करने की कोशिश की:
1)application-context.xml
में, मैं एक विन्यास जो मुझे नाम स्थान की अनदेखी करने की अनुमति होगी के लिए खोज की, लेकिन एक नहीं पा सके:
<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="packagesToScan">
<list>
<value>com.mycompany.mypkg</value>
</list>
</property>
<property name="marshallerProperties">
<map>
<entry key="???"><value type="java.lang.Boolean">false</value></entry>
</map>
</property>
</bean>
ऐसा लगता है कि केवल उपलब्ध विकल्प जैक्सबी 2 मार्शेलर के जावाडोक में सूचीबद्ध हैं:
/**
* Set the JAXB {@code Marshaller} properties. These properties will be set on the
* underlying JAXB {@code Marshaller}, and allow for features such as indentation.
* @param properties the properties
* @see javax.xml.bind.Marshaller#setProperty(String, Object)
* @see javax.xml.bind.Marshaller#JAXB_ENCODING
* @see javax.xml.bind.Marshaller#JAXB_FORMATTED_OUTPUT
* @see javax.xml.bind.Marshaller#JAXB_NO_NAMESPACE_SCHEMA_LOCATION
* @see javax.xml.bind.Marshaller#JAXB_SCHEMA_LOCATION
*/
public void setMarshallerProperties(Map<String, ?> properties) {
this.marshallerProperties = properties;
}
2) मैं भी कोड में unmarshaller कॉन्फ़िगर करने की कोशिश की:
try {
jc = JAXBContext.newInstance("com.mycompany.mypkg");
Unmarshaller u = jc.createUnmarshaller();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(false);//Tried this option.
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(xmlFile.toFile());
u.unmarshal(new DOMSource(doc));
return (Foo)u.unmarshal(new StreamSource(xmlFile.toFile()));
} catch (ParserConfigurationException | SAXException | IOException | JAXBException e) {
LOGGER.error("Erreur Unmarshalling CPL");
}
3) एक SAXParser साथ अलग रूप:
try {
jc = JAXBContext.newInstance("com.mycompany.mypkg");
Unmarshaller um = jc.createUnmarshaller();
final SAXParserFactory sax = SAXParserFactory.newInstance();
sax.setNamespaceAware(false);
final XMLReader reader = sax.newSAXParser().getXMLReader();
final Source er = new SAXSource(reader, new InputSource(new FileReader(xmlFile.toFile())));
return (Foo)um.unmarshal(er);
}catch(...) {[...]}
यह एक काम करता है! लेकिन फिर भी, मैं हर समय इस बदसूरत conf की आवश्यकता के बिना Unmarshaller को स्वत: करने में सक्षम होना पसंद करूंगा।