Jaxb parsing XML to JavaBean ignoring namespaces

	/**
	 * 解析xml(忽略命名空间)
	 * 
	 * @param cla
	 * @param content
	 * @return
	 * @throws JAXBException
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 */
	public static Object unmarshall(Class<?> cla, String content) throws JAXBException, ParserConfigurationException, SAXException {

		JAXBContext jaxbContext = JAXBContext.newInstance(cla);
		Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
		StringReader reader = new StringReader(content);

		SAXParserFactory sax = SAXParserFactory.newInstance();
		sax.setNamespaceAware(false);
		XMLReader xmlReader = sax.newSAXParser().getXMLReader();

		Source source = new SAXSource(xmlReader, new InputSource(reader));
		Object o = unmarshaller.unmarshal(source);

		return o;
	}

Paste the code first

 

Due to various reasons, the URLs of many custom standard namespaces in special industries cannot be accessed, which causes Jaxb to get naming errors when converting XML content into JavaBeans, and cannot parse successfully!

In another case, attributes such as xsi:type and xml:lang appear in the node attributes in XML, which require the object corresponding to the node to match the set type. Originally, this is the behavior of strictly matching the XML node format. A good way; but in actual operation, this greatly increases the data model, especially the production of complex programs for super-large data models. It is hoped that only nodes can be matched with JavaBeans, and the corresponding data can be filled into the data model.

set up

SAXParserFactory.setNamespaceAware(false);

That is, when converting, ignore namespace reading, ignore type checking, and complete data conversion

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325443480&siteId=291194637