java忽略命名空间_java – JAXB:如何在unmarshalling XML文档期间忽略命名空间?

我相信你必须

add the namespace到你的xml文档,用,例如,使用

SAX filter。

这意味着:

>使用一个新类来定义一个ContentHandler接口,它将在JAXB获取它们之前拦截SAX事件。

>定义一个XMLReader,它将设置内容处理程序

然后将这两个链接在一起:

public static Object unmarshallWithFilter(Unmarshaller unmarshaller,

java.io.File source) throws FileNotFoundException, JAXBException

{

FileReader fr = null;

try {

fr = new FileReader(source);

XMLReader reader = new NamespaceFilterXMLReader();

InputSource is = new InputSource(fr);

SAXSource ss = new SAXSource(reader, is);

return unmarshaller.unmarshal(ss);

} catch (SAXException e) {

//not technically a jaxb exception, but close enough

throw new JAXBException(e);

} catch (ParserConfigurationException e) {

//not technically a jaxb exception, but close enough

throw new JAXBException(e);

} finally {

FileUtil.close(fr); //replace with this some safe close method you have

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值