jaxb读取xml时忽略命名空间解决读取时报错问题

在xml时一般我们会加上xsd等约束和命名空间,加上了有时会导致jaxb读取xml报错,众里寻他千百度,那人却在灯火阑珊处,经过一番苦寻找到了解决方法,忽略命名空间的方式读取xml,实例代码如下:

package xxx;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.sax.SAXSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLFilterImpl;
import org.xml.sax.helpers.XMLReaderFactory;

public class XmlOperator {

 /**
     * 读取XML-可配置忽略命名空间
     * 
     * @param file XML文件
     * @param type 类型
     * @param <T> 数据类型
     * @param ignoreNamespace 解析时是否忽略命名空间
     * @return 数据对象
     * @author chang.xu
     */
    @SuppressWarnings("unchecked")
    public <T> T readXml(final File file, final Class<T> type, final boolean ignoreNamespace) {
        T objResult = null;
        FileInputStream fis = null;
        try {
            JAXBContext context = JAXBContext.newInstance(type);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            
            XMLReader reader = XMLReaderFactory.createXMLReader();
            XMLFilterImpl nsfFilter = new XMLFilterImpl() {
                
                @Override
                public void startDocument() throws SAXException {
                    super.startDocument();
                }
                
                @Override
                public void startElement(String uri, String localName, String qName, Attributes atts)
                    throws SAXException {
                    String strURI = uri;
                    if (ignoreNamespace) {
                        strURI = "";
                    }
                    super.startElement(strURI, localName, qName, atts);
                }
                
                @Override
                public void endElement(String uri, String localName, String qName) throws SAXException {
                    String strURI = uri;
                    if (ignoreNamespace) {
                        strURI = "";
                    }
                    super.endElement(strURI, localName, localName);
                }
                
                @Override
                public void startPrefixMapping(String prefix, String url) throws SAXException {
                    super.startPrefixMapping(prefix, url);
                }
            };
            fis = new FileInputStream(file);
            nsfFilter.setParent(reader);
            InputSource input = new InputSource(fis);
            SAXSource source = new SAXSource(nsfFilter, input);
            objResult = (T) unmarshaller.unmarshal(source);
        } catch (JAXBException | SAXException | FileNotFoundException e) {
            LOG.error("读取文件数据出错!file:" + file.getPath() + " type:" + type, e);
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    LOG.error("文件流关闭异常:" + e.getMessage(), e);
                }
            }
        }
        return objResult;
    }
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值