1

XML データを新しく作成した Java コンテンツ ツリーにデシリアライズしようとしています。

私は SAX を使用しており、src\main\java\Summaries.javaの下に 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);
}

次のようなエラーが表示されます。

javax.xml.bind.UnmarshalException
 - with linked exception:

[org.xml.sax.SAXParseException: File "null" not found.]
     at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:315)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:505)
     at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:206)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:173)
     at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:120)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:103)
     at main.java.RESTtestclient.marshallJAXB(RESTtestclient.java:346)
    at main.java.RESTtestclient.main(RESTtestclient.java:82)

Caused by: org.xml.sax.SAXParseException: File "null" not found.
     at org.apache.xerces.framework.XMLParser.reportError(XMLParser.java:1156)
    at org.apache.xerces.readers.DefaultEntityHandler.startReadingFromDocument(DefaultEntityHandler.java:499)
     at org.apache.xerces.framework.XMLParser.parseSomeSetup(XMLParser.java:310)
    at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:1034)
     at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:200)

私を正しい方向に向ける可能性のあるアイデアはありますか?

4

1 に答える 1

6

JAXBを使用して次のアプローチを試すことができますUnmarshallerHandler

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);
    }

}
于 2012-06-05T09:01:52.197 に答える