局部解组与JAXB [英] Partial-unmarshalling with JAXB

查看:188
本文介绍了局部解组与JAXB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的大XML的部分拆封。

XML具有以下结构:

 <唱片和GT;
    <合同>
        ...
    < /合同>
    <合同>
        ...
    < /合同>
    ...
    <合同>
        ...
    < /合同>
    <合同>
        ...
    < /合同>
< /唱片和GT;

和与XJC产生的结果类:

   - 记录
    | - 合同

如果我按照<一href=\"http://stackoverflow.com/questions/6484681/jaxb-partial-unmarshalling-elements-without-xmlrootelement\">these(sample从JAXB里),我得到错误:

 异常线程mainjavax.xml.bind.UnmarshalException:意外的元素(URI:http://somedomain.com,当地的合同)。预计元素&LT; {} http://somedomain.com唱片和GT;

如果我使用:

 &LT; JAXB:globalBindings localScoping =顶层/&GT;

我得到错误:

  org.xml.sax.SAXParseException:具有相同的名称com.my.package.TextA类/接口已在使用。使用定制类来解决这个矛盾。

但我需要改变很多类。这是不是解决办法。


解决方案

  / **
 *用户:r.ibragimov
 *日期:13年6月4日
 * /
公共类PartialJAXB1 {    公共静态无效的主要(字串[] args)抛出JAXBException,XMLStreamException,FileNotFoundException异常{        最终的QName QNAME =新QName(http://www.domain.com,合同);        的InputStream的InputStream =新的FileInputStream(新文件(C:\\\\的test.xml));        //创建输入流XML事件读卡器
        的XMLInputFactory的XMLInputFactory = XMLInputFactory.newInstance();
        XMLEventReader的XMLEventReader的= xmlInputFactory.createXMLEventReader(InputStream的);        //初始化JAXB
        JAXBContext而上下文= JAXBContext.newInstance(Records.class);
        解组解组= context.createUnmarshaller();        XMLEvent的E = NULL;        //循环尽管XML流
        而((E = xmlEventReader.peek())!= NULL){            //检查事件是一个文档开始元素
            如果(e.isStartElement()及和放大器;((StartElement的)E).getName()等于(QNAME)){                //和解组文档
                Records.Contract合同= unmarshaller.unmarshal(XMLEventReader的,Records.Contract.class).getValue();
                的System.out.println(合同);
            }其他{
                xmlEventReader.next();
            }        }    }
}

I want do partial unmarshaling of big XML.

XML has following structure:

<Records>
    <Contract>
        ...
    </Contract>
    <Contract>
        ...
    </Contract>
    ...
    <Contract>
        ...
    </Contract>
    <Contract>
        ...
    </Contract>
</Records>

And result class generated with XJC:

- Records
    |- Contract

If i follow these(sample from jaxb-ri), i get error:

Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"http://somedomain.com", local:"Contract"). Expected elements are <{http://somedomain.com}Records>

If i use:

<jaxb:globalBindings localScoping="toplevel"/>

I get error:

org.xml.sax.SAXParseException: A class/interface with the same name "com.my.package.Text" is already in use. Use a class customization to resolve this conflict.

But i need change many classes. And this is not solution.

解决方案

/**
 * User: r.ibragimov
 * Date: 04.06.13
 */
public class PartialJAXB1 {

    public static void main(String[] args) throws JAXBException, XMLStreamException, FileNotFoundException {

        final QName qName = new QName("http://www.domain.com","Contract");

        InputStream inputStream = new FileInputStream(new File("c:\\test.xml"));

        // create xml event reader for input stream
        XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
        XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(inputStream);

        // initialize jaxb
        JAXBContext context = JAXBContext.newInstance(Records.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();

        XMLEvent e = null;

        // loop though the xml stream
        while( (e = xmlEventReader.peek()) != null ) {

            // check the event is a Document start element
            if(e.isStartElement() && ((StartElement)e).getName().equals(qName)) {

                // unmarshall the document
                Records.Contract contract = unmarshaller.unmarshal(xmlEventReader, Records.Contract.class).getValue();
                System.out.println(contract);
            } else {
                xmlEventReader.next();
            }

        }

    }
}

这篇关于局部解组与JAXB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆