Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial-unmarshalling with JAXB

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.

like image 402
Ruslan Avatar asked Jun 04 '13 09:06

Ruslan


People also ask

What is JAXB marshalling and unmarshalling?

JAXB definitionsMarshalling is the process of transforming Java objects into XML documents. Unmarshalling is the process of reading XML documents into Java objects. The JAXBContext class provides the client's entry point to the JAXB API.

How does JAXB marshalling work?

In JAXB, marshalling involves parsing an XML content object tree and writing out an XML document that is an accurate representation of the original XML document, and is valid with respect the source schema. JAXB can marshal XML data to XML documents, SAX content handlers, and DOM nodes.

What is ObjectFactory in JAXB?

jaxb package. An ObjectFactory allows you to programatically construct new instances of the Java representation for XML content. The Java representation of XML content can consist of schema derived interfaces and classes representing the binding of schema type definitions, element declarations and model groups.


1 Answers

/**
 * 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();
            }

        }

    }
}
like image 191
Ruslan Avatar answered Sep 27 '22 15:09

Ruslan