我使用Jaxb2和Spring。我正試圖解組一些由我的客戶發送的XML。Jaxb忽略unmarshalling上的命名空間
到現在爲止,我只需要處理一個客戶它派了一些這樣的XML:
<foo xmlns="com.acme">
<bar>[...]</bar>
<foo>
綁定到一個POJO是這樣的:
@XmlType(name = "", propOrder = {"bar"})
@XmlRootElement(name = "Foo")
public class Foo {
@XmlElement(name = "Bar")
private String bar;
[...]
}
我發現,以前的開發人員對unmarshaller中的命名空間進行了硬編碼,以使其工作。
現在,第二個客戶發送相同的XML但改變了命名空間!
<foo xmlns="com.xyz">
<bar>[...]</bar>
<foo>
顯然,解組未能解組,因爲它預計一些{com.acme}foo
代替{com.xyz}foo
。不幸的是,要求客戶更改XML不是一種選擇。
我的嘗試:
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>
似乎唯一可用的選項是Jaxb2Marshaller的Javadoc中列出的選項:
/**
* 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)我也試過配置解組代碼:
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(...) {[...]}
這一個工程!但是,我仍然希望能夠自動調用Unmarshaller,而不需要每次都需要這個難看的配置。