将SAX与JAXBContext结合使用

将SAX与JAXBContext结合使用,jaxb,sax,saxparser,Jaxb,Sax,Saxparser,我正在尝试将XML数据反序列化到新创建的Java内容树中: 我正在使用SAX,将Java类放在src\main\Java\Summaries.Java下,并试图简单地打印提取的文档: String xmlPath = "C:\\workspace-sts-2.8.0.RELEASE\\RESTClient\\src\\main\\resources\\dailySummary.xml"; String xmlPath2 = "/dailySummary.xml"; String xmlPath3

我正在尝试将XML数据反序列化到新创建的Java内容树中:

我正在使用SAX,将Java类放在src\main\Java\Summaries.Java下,并试图简单地打印提取的文档:

String xmlPath = "C:\\workspace-sts-2.8.0.RELEASE\\RESTClient\\src\\main\\resources\\dailySummary.xml";
String xmlPath2 = "/dailySummary.xml";
String xmlPath3 = "/src/main/resources/dailySummary.xml";

InputStream inputStream = null;
InputSource inputSource = null;
Unmarshaller unmarshaller = null;
JAXBContext jc = null;
try 
{

// read from a file
// or try xmlPath1 or try xmlPath3
inputStream = RESTtestclient.class.getClassLoader().getResourceAsStream(xmlPath2);

inputSource = new InputSource(inputStream);
SAXParserFactory sax = SAXParserFactory.newInstance();
sax.setNamespaceAware(true);
XMLReader reader = sax.newSAXParser().getXMLReader();
SAXSource saxSource= new SAXSource(reader, inputSource);

// use jAXB
Class[] classes = new Class[1];
classes[0]= Summaries.class;
jc = JAXBContext.newInstance(classes);
unmarshaller = jc.createUnmarshaller();

@SuppressWarnings("unchecked")
**JAXBElement<Object> doc = (JAXBElement<Object>) unmarshaller.unmarshal(saxSource);**  // errors out here
sysout(doc);
}

有什么想法可以给我指出正确的方向吗

您可以使用JAXB的
解组器
尝试以下方法:

package forum10890323;

import java.io.InputStream;
import javax.xml.bind.*;
import javax.xml.parsers.*;
import org.xml.sax.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);
        UnmarshallerHandler unmarshallerHandler = jc.createUnmarshaller().getUnmarshallerHandler();

        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        xr.setContentHandler(unmarshallerHandler);

        InputStream inputStream = Root.class.getClassLoader().getResourceAsStream("forum10890323/input.xml");
        InputSource inputSource = new InputSource(inputStream);
        xr.parse(inputSource);
        inputStream.close();

        Root root = (Root) unmarshallerHandler.getResult();

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }

}

您可以使用JAXB的
解组器
尝试以下方法:

package forum10890323;

import java.io.InputStream;
import javax.xml.bind.*;
import javax.xml.parsers.*;
import org.xml.sax.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);
        UnmarshallerHandler unmarshallerHandler = jc.createUnmarshaller().getUnmarshallerHandler();

        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        xr.setContentHandler(unmarshallerHandler);

        InputStream inputStream = Root.class.getClassLoader().getResourceAsStream("forum10890323/input.xml");
        InputSource inputSource = new InputSource(inputStream);
        xr.parse(inputSource);
        inputStream.close();

        Root root = (Root) unmarshallerHandler.getResult();

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }

}